<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="Keywords" content="Zope, Plone, bfg, 开源社区">
<meta content="这里是Zope开源web应用服务器和Plone开源内容管理系统的中文技术社区。" name="description">
<!-- Plone ECMAScripts --><style type="text/css">
            .tabber{display:none}
        </style>
<style type="text/css">
            <!-- @import url(http://everydo.com/themes/czug.org/css/ploneStyles6234.css); -->
        </style>
<link rel="alternate stylesheet" type="text/css" media="screen" href="http://everydo.com/themes/czug.org/css/ploneStyles0053.css" title="Small Text">
<link rel="alternate stylesheet" type="text/css" media="screen" href="http://everydo.com/themes/czug.org/css/ploneStyles2144.css" title="Large Text">
<style type="text/css" media="all">
            <!-- @import url(http://everydo.com/themes/czug.org/css/ploneStyles8099.css); -->
        </style>
<link rel="stylesheet" href="http://everydo.com/themes/czug.org/css/navtree.css" type="text/css">
<script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/sarissa.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/base2-dom-fp.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/kukit.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/zopen.js">
        </script><link type="text/css" rel="kinetic-stylesheet" href="http://czug.org/static/navtree.kss" media="all">
<!-- Internet Explorer CSS Fixes --><!--[if IE]>
            <style type="text/css" media="all">
                @import url(IEFixes.css);
            </style>
        <![endif]--><link rel="shortcut icon" type="image/x-icon" href="http://everydo.com/themes/czug.org/img/favicon.ico">
<link rel="home" href="http://czug.org" title="Front page">
<link rel="search" href="http://everydo.com/themes/czug.org/search_form" title="Search this site">
<link rel="author" href="http://everydo.com/themes/czug.org/author/panjy" title="Author information">
<link rel="contents" href="http://everydo.com/sitemap" title="Site Map">
<!-- Disable IE6 image toolbar --><meta http-equiv="imagetoolbar" content="no">
<title>第十一章 使用sizer放置构件 - 中国Zope/Plone用户组</title>
</head>
<body class="section-index_html" dir="ltr">
        <div id="visual-portal-wrapper">
            <div id="portal-top">
                <div id="portal-header">
                    <p class="hiddenStructure">
                        <a accesskey="2" href="http://everydo.com/themes/czug.org/index.html?notheme#documentContent">
                            Skip to content.
                        </a>
                        |
                        <a accesskey="6" href="http://everydo.com/themes/czug.org/index.html?notheme#portlet-navigation-tree">
                            Skip to navigation
                        </a>
                    </p>
                    <h1 id="portal-logo">
                        <a href="http://czug.org" accesskey="1">
                            中国Zope/Plone用户组
                        </a>
                    </h1>
                    <div id="portal-skinswitcher">
                    </div>
                    <h5 class="hiddenStructure">
                        Sections
                    </h5>
                    <ul id="portal-globalnav">
<li id="首页" class="plain">
                        <a href="/">首页</a>
                      </li>
<li id="Zope" class="plain">
                        <a href="/zope">Zope</a>
                      </li>
<li id="Plone" class="plain">
                        <a href="/plone">Plone</a>
                      </li>
<li id="Python" class="selected">
                        <a href="/python">Python</a>
                      </li>
<li id="社区项目" class="plain">
                        <a href="/projects">社区项目</a>
                      </li>
<li id="博客群" class="plain">
                        <a href="/blog/">博客群</a>
                      </li>
<li id="关于" class="plain">
                        <a href="/about">关于</a>
                      </li>
</ul>
</div>
                <div id="portal-personaltools-wrapper">
                    <h5 class="hiddenStructure">
                        Personal tools
                    </h5>
                </div>
                <!-- <div id="portal-breadcrumbs">
                <span id="breadcrumbs-you-are-here">You
                are here:</span>
                <a href="http://czug.org">Home</a>
                </div>
                -->
            </div>
            <div class="visualClear" id="clear-space-before-wrapper-table">
                <!-- -->
            </div>
            <table id="portal-columns"><tbody><tr id="portal-tr">
<td id="portal-column-one">
            <div class="visualPadding"> <div class="document">

<div id="leftcolumn" class="edoBox">
  <b class="xtop edoxtop">
    <b class="xb1"></b>
    <b class="xb2"></b>
    <b class="xb3"></b>
    <b class="xb4"></b>
 </b>
 <b class="itemxtop edoitemxtop">
    <b class="itemxb1"></b>
    <b class="itemxb2"></b>
    <b class="itemxb3"></b>
    <b class="itemxb4"></b>
 </b>
 <dl class="portlet portlet-navigation-tree">
<dt class="portletHeader">
        <a class="title" href="http://www.czug.org/python/wxpythoninaction/">《活学活用wxPython》</a>
    </dt>
    <dd class="portletItem">
        <ul class="portletNavigationTree navTreeLevel0">
<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/pr01.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="pr01.htm" href="http://www.czug.org/python/wxpythoninaction/pr01.htm" onfocus="this.blur()">
        
        
        前言
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/pr02.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="pr02.htm" href="http://www.czug.org/python/wxpythoninaction/pr02.htm" onfocus="this.blur()">
        
        
        关于本书
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch01.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch01.htm" href="http://www.czug.org/python/wxpythoninaction/ch01.htm" onfocus="this.blur()">
        
        
        第一章 欢迎使用wxPython
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch02.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch02.htm" href="http://www.czug.org/python/wxpythoninaction/ch02.htm" onfocus="this.blur()">
        
        
        第二章 给wxPython程序一个坚实的基础
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch03.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch03.htm" href="http://www.czug.org/python/wxpythoninaction/ch03.htm" onfocus="this.blur()">
        
        
        第三章 在事件驱动环境中开发
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch04.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch04.htm" href="http://www.czug.org/python/wxpythoninaction/ch04.htm" onfocus="this.blur()">
        
        
        第四章 用PyCrust使得wxPython更易处理
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch05.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch05.htm" href="http://www.czug.org/python/wxpythoninaction/ch05.htm" onfocus="this.blur()">
        
        
        第五章 绘制蓝图
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch06.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch06.htm" href="http://www.czug.org/python/wxpythoninaction/ch06.htm" onfocus="this.blur()">
        
        
        第六章 使用wxPython基本构件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch07.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch07.htm" href="http://www.czug.org/python/wxpythoninaction/ch07.htm" onfocus="this.blur()">
        
        
        第七章 使用基础控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch08.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch08.htm" href="http://www.czug.org/python/wxpythoninaction/ch08.htm" onfocus="this.blur()">
        
        
        第八章 将构件放入窗体中
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch09.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch09.htm" href="http://www.czug.org/python/wxpythoninaction/ch09.htm" onfocus="this.blur()">
        
        
        第九章 通过对话框让用户选择
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch10.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch10.htm" href="http://www.czug.org/python/wxpythoninaction/ch10.htm" onfocus="this.blur()">
        
        
        第十章 创建和使用wxPython菜单
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node selected notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch11.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        navTreeCurrentItem" name="ch11.htm" href="http://www.czug.org/python/wxpythoninaction/ch11.htm" onfocus="this.blur()">
        
        
        第十一章 使用sizer放置构件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch12.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch12.htm" href="http://www.czug.org/python/wxpythoninaction/ch12.htm" onfocus="this.blur()">
        
        
        第十二章 操作基本图像
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch13.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch13.htm" href="http://www.czug.org/python/wxpythoninaction/ch13.htm" onfocus="this.blur()">
        
        
        第十三章 建造列表控件并管理列表项
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch14.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch14.htm" href="http://www.czug.org/python/wxpythoninaction/ch14.htm" onfocus="this.blur()">
        
        
        第十四章 网格控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch15.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch15.htm" href="http://www.czug.org/python/wxpythoninaction/ch15.htm" onfocus="this.blur()">
        
        
        第十五章 树形控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch16.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch16.htm" href="http://www.czug.org/python/wxpythoninaction/ch16.htm" onfocus="this.blur()">
        
        
        第十六章 在应用程序中加入HTML
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch17.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch17.htm" href="http://www.czug.org/python/wxpythoninaction/ch17.htm" onfocus="this.blur()">
        
        
        第十七章 wxPython的打印构架
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch18.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch18.htm" href="http://www.czug.org/python/wxpythoninaction/ch18.htm" onfocus="this.blur()">
        
        
        第十八章 使用wxPython的其他功能
            </a>
    </div>
    
</li>

            <!--p class="navTreeItem visualNoMarker" 
               tal:repeat="node items" 
               tal:replace="structure node">
            </p-->
        </ul>
</dd>
    <dd class="portletFooter">
        <div><!-- --></div>
    </dd>
  </dl>
<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>
</div>
</div>
 </div> </td>
<td id="portal-column-content">
                            <div id="content" class="documentContent">
<h1>第十一章 使用sizer放置构件</h1>
<div lang="en" id="content" dir="ltr">
<span id="top" class="anchor"></span>
<span id="line-1" class="anchor"></span><p class="line867"></p>
<div class="table-of-contents">
<p class="table-of-contents-heading">目录</p>
<ol><li>
<a href="#head-8d4cf8b6261506b5d3e31cd0dd2b8531eb472183">使用sizer放置窗口部件</a><ol>
<li><a href="#head-50e81fa6de7d591a5bad7a766c581650a12d1861">sizer是什么？</a></li>
<li>
<a href="#head-4ed462861bb1109f9e643c41f618c45281907eea">基本的sizer：grid</a><ol>
<li><a href="#head-29585f4144075dedc5740e748c8235c46e5bb5f8">什么是grid</a></li>
<li><a href="#head-efdb330edd8b6cd2d0488f8dba1a59362c7778c4">如何对sizer添加或移除孩子？</a></li>
<li><a href="#head-425f88a0912efe2e3ba3413df4c152c5ab7e649b">sizer是如何管理它的孩子的尺寸和对齐的？</a></li>
<li><a href="#head-95ff96bce5a752f8ec342c24259e635e8f335f5c">能够为sizer或它的孩子指定一个最小的尺寸吗？</a></li>
<li><a href="#head-b2f340a3d187a3c74bea1e712be145adfa0905ef">sizer如何管理每个孩子的边框？</a></li>
</ol>
</li>
<li>
<a href="#head-ae9b93628558f04bdbc27a01d4b17fb924e883d2">使用其它类型的sizer</a><ol>
<li><a href="#head-1bfb417861035f00bd754d8ab441cbe05079f952">什么是flex grid  sizer？</a></li>
<li><a href="#head-bc0d65d65db8f2a18cadf007de59be462007ce27">什么是grid bag  sizer?</a></li>
<li><a href="#head-af09e9c27edb728c13c67129cd09286460d07941">什么是box</a></li>
<li><a href="#head-bfea748837095776adb31cdddd7c19dd11768192">什么是static</a></li>
</ol>
</li>
<li><a href="#head-11ef90c5b495f9c562241fdf96022be5470ff08a">一个现实中使用sizer的例子</a></li>
<li><a href="#head-378681f27ad089b21bdca12c74de21e67465a840">本章小结</a></li>
</ol>
</li></ol>
</div> <span id="line-2" class="anchor"></span><span id="line-3" class="anchor"></span><p class="line867">
</p>
<h1 id="head-8d4cf8b6261506b5d3e31cd0dd2b8531eb472183">1. 使用sizer放置窗口部件</h1>
<span id="line-4" class="anchor"></span><p class="line867"><strong>本章内容</strong> <span id="line-5" class="anchor"></span><span id="line-6" class="anchor"></span></p>
<p class="line862">·理解<tt>sizer</tt> <span id="line-7" class="anchor"></span><span id="line-8" class="anchor"></span></p>
<p class="line862">·使用<tt>sizer</tt>分隔窗口部件 <span id="line-9" class="anchor"></span><span id="line-10" class="anchor"></span></p>
<p class="line862">·使用<tt>sizer</tt>的<tt>grid</tt>系列 <span id="line-11" class="anchor"></span><span id="line-12" class="anchor"></span></p>
<p class="line862">·使用<tt>box</tt> <tt>sizer</tt> <span id="line-13" class="anchor"></span><span id="line-14" class="anchor"></span></p>
<p class="line862">·看看<tt>sizer</tt>的实际应用 <span id="line-15" class="anchor"></span><span id="line-16" class="anchor"></span></p>
<p class="line874">传统上，在用户界面编程中的最苦恼的一个问题是管理窗口中的窗口部件的实际布局。因为它涉及到与绝对位置直接打交到，这是很痛苦的一件事情。 <span id="line-17" class="anchor"></span><span id="line-18" class="anchor"></span></p>
<p class="line862">所以你需要一个结构，它根据预设的模式来决定如何调整和移动窗口部件。目前推荐用来处理复杂布局的方法是使用<tt>sizer</tt>。<tt>sizer</tt>是用于自动布局一组窗口部件的算法。<tt>sizer</tt>被附加到一个容器，通常是一个框架或面板。在父容器中创建的子窗口部件必须被分别地添加到<tt>sizer</tt>。当<tt>sizer</tt>被附加到容器时，它随后就管理它所包含的孩子的布局。 <span id="line-19" class="anchor"></span><span id="line-20" class="anchor"></span></p>
<p class="line862">使用<tt>sizer</tt>的好处是很多的。当子窗口部件的容器的尺寸改变时，<tt>sizer</tt>将自动计算它的孩子的布局并作出相应的调整。同样，如果其中的一个孩子改变了尺寸，那么<tt>sizer</tt>能够自动地刷新布局。此外，当你想要改变布局时，<tt>sizer</tt>管理起来很容易。最大的弊端是<tt>sizer</tt>的布局有一些局限性。但是，最灵活<tt>sizer</tt>——<tt>grid</tt> <tt>bag</tt>和<tt>box</tt>，能够做你要它们做的几乎任何事。 <span id="line-21" class="anchor"></span><span id="line-22" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-50e81fa6de7d591a5bad7a766c581650a12d1861">1.1. sizer是什么？</h2>
<span id="line-23" class="anchor"></span><p class="line862">一个<tt>wxPython</tt> <tt>sizer</tt>是一个对象，它唯一的目的就是管理容器中的窗口部件的布局。<tt>sizer</tt>本身不是一个容器或一个窗口部件。它只是一个屏幕布局的算法。所有的<tt>sizer</tt>都是抽象类<tt>wx.Sizer</tt>的一个子类的实例。<tt>wxPython</tt>提供了5个<tt>sizer</tt>，定义在表11.1中。<tt>sizer</tt>可以被放置到别的<tt>sizer</tt>中以达到更灵活的管理。 <span id="line-24" class="anchor"></span><span id="line-25" class="anchor"></span></p>
<p class="line867"><strong> </strong> <span id="line-26" class="anchor"></span><span id="line-27" class="anchor"></span></p>
<p class="line867"><strong>表11.1</strong>  <strong><tt>wxPython</tt>中预定义的<tt>sizer</tt></strong> <span id="line-28" class="anchor"></span><span id="line-29" class="anchor"></span></p>
<p class="line867"><tt>Grid</tt>：一个十分基础的网格布局。当你要放置的窗口部件都是同样的尺寸且整齐地放入一个规则的网格中是使用它。 <span id="line-30" class="anchor"></span><span id="line-31" class="anchor"></span></p>
<p class="line867"><tt>Flex</tt> <tt>grid</tt>：对<tt>grid</tt> <tt>sizer</tt>稍微做了些改变，当窗口部件有不同的尺寸时，可以有更好的结果。 <span id="line-32" class="anchor"></span><span id="line-33" class="anchor"></span></p>
<p class="line867"><tt>Grid</tt> <tt>bag</tt>：<tt>grid</tt> <tt>sizer</tt>系列中最灵活的成员。使得网格中的窗口部件可以更随意的放置。 <span id="line-34" class="anchor"></span><span id="line-35" class="anchor"></span></p>
<p class="line867"><tt>Box</tt>：在一条水平或垂直线上的窗口部件的布局。当尺寸改变时，在控制窗口部件的的行为上很灵活。通常用于嵌套的样式。可用于几乎任何类型的布局。
<span id="line-36" class="anchor"></span><span id="line-37" class="anchor"></span></p>
<p class="line867"><tt>Static</tt> <tt>box</tt>：一个标准的<tt>box</tt> <tt>sizer</tt>。带有标题和环线。 <span id="line-38" class="anchor"></span><span id="line-39" class="anchor"></span></p>
<p class="line862">如果你想要你的布局类似<tt>grid</tt>或<tt>box</tt>,<tt>wxPython</tt>可以变通；实际上，任何有效的布局都能够被想像为一个<tt>grid</tt>或一系列<tt>box</tt>。 <span id="line-40" class="anchor"></span><span id="line-41" class="anchor"></span></p>
<p class="line862">所有的<tt>sizer</tt>都知道它们的每个孩子的最小尺寸。通常，<tt>sizer</tt>也允许有关布局的额外的信息，例如窗口部件之间有多少空间，它能够使一个窗口部件的尺寸增加多以填充空间，以及当窗口部件比分配给它们的空间小时如何对齐这些窗口部件等。根据这些少量的信息<tt>sizer</tt>用它的布局算法来确定每个孩子的尺寸和位置。<tt>wxPython</tt>中的每种<tt>sizer</tt>对于同组子窗口部件所产生的最终布局是不同的。 <span id="line-42" class="anchor"></span><span id="line-43" class="anchor"></span></p>
<p class="line862">贯穿本章，你都会看到，我们使用非常相似的布局来演示每个<tt>sizer</tt>类型。 <span id="line-44" class="anchor"></span><span id="line-45" class="anchor"></span></p>
<p class="line862">下面是使用一个<tt>sizer</tt>的三个基本步骤： <span id="line-46" class="anchor"></span><span id="line-47" class="anchor"></span></p>
<ul>
<li><p class="line862">创建并关联<tt>sizer</tt>到一个容器。<tt>sizer</tt>被关联到容器使用<tt>wx.Window</tt>的<tt>SetSizer(sizer)</tt>方法。由于这是一个<tt>wx.Window</tt>的方法，所以这意味着任何<tt>wxPython</tt>窗口部件都可以有一个<tt>sizer</tt>，尽管<tt>sizer</tt>只对容器类的窗口部件有意义。 <span id="line-48" class="anchor"></span></p></li>
<li><p class="line862">添加每个孩子到这个<tt>sizer</tt>。所有的孩子窗口部件需要被单独添加到该<tt>sizer</tt>。仅仅创建使用容器作为父亲的孩子窗口部件是不够的。还要将孩子窗口部件添加到一个<tt>sizer</tt>，这个主要的方法是<tt>Add()</tt>。<tt>Add()</tt>方法有一对不同的标记，我们将在下一节讨论。 <span id="line-49" class="anchor"></span></p></li>
<li><p class="line862">（可选的）使<tt>sizer</tt>能够计算它的尺寸。告诉<tt>sizer</tt>去根据它的孩子来计算它的尺寸，这通过在父窗口对象上调用<tt>wx.Window</tt>的<tt>Fit()</tt>方法或该<tt>sizer</tt>的<tt>Fit(window)</tt>方法。（这个窗口方法重定向到<tt>sizer</tt>方法。）两种情况下，这个<tt>Fit()</tt>方法都要求<tt>sizer</tt>根据它所掌握的它的孩子的情况去计算它的尺寸，并且它调整父窗口部件到合适的尺寸。还有一个相关的方法：<tt>FitInside()</tt>，它不改变父窗口部件的显示尺寸，但是它改变它虚拟尺寸——这意味着如果窗口部件是在一个可滚动的面板中，那么<tt>wxPython</tt>会重新计算是否需要滚动条。 <span id="line-50" class="anchor"></span></p></li>
</ul>
<p class="line862">我们既要讨论特定的<tt>sizer</tt>的行为，也要讨论所有<tt>sizer</tt>的共同行为。这就有个先后的问题。我们将以介绍<tt>grid</tt>
<tt>sizer</tt>作为开始，它是最容易理解的。之后，我们将讨论所有<tt>sizer</tt>的共同行为，使用<tt>grid</tt> <tt>sizer</tt>作为一个例子。（使用<tt>grid</tt> <tt>sizer</tt>作为例子使得最共同的行为更形象化。）之后我们将讨论其它的特定类型的<tt>sizer</tt>。 <span id="line-51" class="anchor"></span><span id="line-52" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-4ed462861bb1109f9e643c41f618c45281907eea">1.2. 基本的sizer：grid</h2>
<span id="line-53" class="anchor"></span><p class="line867"><strong><tt>sizer</tt></strong> <span id="line-54" class="anchor"></span><span id="line-55" class="anchor"></span></p>
<p class="line862">后面所有的例子使用了一个有点无聊的窗口部件，目的是占据布局中的空间，这样你可以看到<tt>sizer</tt>是如何工作的。例11.1给出了该窗口部件的代码，它被本章中的其余的例子导入。从始至终，你将看到它的大量的图片——它基本上是一个带有标签的简单的矩形。 <span id="line-56" class="anchor"></span><span id="line-57" class="anchor"></span></p>
<p class="line867"><strong>例11.1</strong>  <strong>块状窗口，在后面的例子中用作一个窗口部件</strong> <span id="line-58" class="anchor"></span><span id="line-59" class="anchor"></span></p>
<p class="line867"><span id="line-60" class="anchor"></span><span id="line-61" class="anchor"></span><span id="line-62" class="anchor"></span><span id="line-63" class="anchor"></span><span id="line-64" class="anchor"></span><span id="line-65" class="anchor"></span><span id="line-66" class="anchor"></span><span id="line-67" class="anchor"></span><span id="line-68" class="anchor"></span><span id="line-69" class="anchor"></span><span id="line-70" class="anchor"></span><span id="line-71" class="anchor"></span><span id="line-72" class="anchor"></span><span id="line-73" class="anchor"></span><span id="line-74" class="anchor"></span><span id="line-75" class="anchor"></span><span id="line-76" class="anchor"></span><span id="line-77" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
function isnumbered(obj) {
return obj.childNodes.length && obj.firstChild.childNodes.length && obj.firstChild.firstChild.className == 'LineNumber';
}
function nformat(num,chrs,add) {
var nlen = Math.max(0,chrs-(''+num).length), res = '';
while (nlen>0) { res += ' '; nlen-- }
return res+num+add;
}
function addnumber(did, nstart, nstep) {
var c = document.getElementById(did), l = c.firstChild, n = 1;
if (!isnumbered(c))
if (typeof nstart == 'undefined') nstart = 1;
if (typeof nstep  == 'undefined') nstep = 1;
n = nstart;
while (l != null) {
if (l.tagName == 'SPAN') {
var s = document.createElement('SPAN');
s.className = 'LineNumber'
s.appendChild(document.createTextNode(nformat(n,4,' ')));
n += nstep;
if (l.childNodes.length)
l.insertBefore(s, l.firstChild)
else
l.appendChild(s)
}
l = l.nextSibling;
}
return false;
}
function remnumber(did) {
var c = document.getElementById(did), l = c.firstChild;
if (isnumbered(c))
while (l != null) {
if (l.tagName == 'SPAN' && l.firstChild.className == 'LineNumber') l.removeChild(l.firstChild);
l = l.nextSibling;
}
return false;
}
function togglenumber(did, nstart, nstep) {
var c = document.getElementById(did);
if (isnumbered(c)) {
remnumber(did);
} else {
addnumber(did,nstart,nstep);
}
return false;
}
</script><script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-872a7751352c876ab5922edf716f328ea85eb15c_000\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-872a7751352c876ab5922edf716f328ea85eb15c_000', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-872a7751352c876ab5922edf716f328ea85eb15c_000" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">class</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="ID">ID</span><span class="Operator">=</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="String">""</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   4 </span>                 <span class="ID">pos</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">DefaultPosition</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">100</span><span class="Operator">,</span> <span class="Number">25</span><span class="Operator">)</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="ID">ID</span><span class="Operator">,</span> <span class="ID">pos</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   6 </span>                          <span class="ID">wx</span><span class="Operator">.</span><span class="ID">RAISED_BORDER</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">label</span> <span class="Operator">=</span> <span class="ID">label</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetBackgroundColour</span><span class="Operator">(</span><span class="String">"white"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   9 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetMinSize</span><span class="Operator">(</span><span class="ID">size</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_PAINT</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnPaint</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span>    <span class="ResWord">def</span> <span class="ID">OnPaint</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">sz</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">GetClientSize</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">dc</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PaintDC</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">w</span><span class="Operator">,</span><span class="ID">h</span> <span class="Operator">=</span> <span class="ID">dc</span><span class="Operator">.</span><span class="ID">GetTextExtent</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">SetFont</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">GetFont</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">DrawText</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">label</span><span class="Operator">,</span> <span class="Operator">(</span><span class="ID">sz</span><span class="Operator">.</span><span class="ID">width</span><span class="Operator">-</span><span class="ID">w</span><span class="Operator">)</span><span class="Operator">/</span><span class="Number">2</span><span class="Operator">,</span> <span class="Operator">(</span><span class="ID">sz</span><span class="Operator">.</span><span class="ID">height</span><span class="Operator">-</span><span class="ID">h</span><span class="Operator">)</span><span class="Operator">/</span><span class="Number">2</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-78" class="anchor"></span><p class="line862">贯穿本章，我们将使用不同的<tt>sizer</tt>来在一个框架中放上几个这样的块窗口部件。我们将使用<tt>grid</tt> <tt>sizer</tt>作为开始。 <span id="line-79" class="anchor"></span><span id="line-80" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-29585f4144075dedc5740e748c8235c46e5bb5f8">1.2.1. 什么是grid</h3>
<span id="line-81" class="anchor"></span><p class="line867"><strong><tt>sizer</tt>？</strong> <span id="line-82" class="anchor"></span><span id="line-83" class="anchor"></span></p>
<p class="line867"><tt>wxPython</tt>提供的最简单的<tt>sizer</tt>是<tt>grid</tt>。顾名思义，一个<tt>grid</tt> <tt>sizer</tt>把它的孩子放置在一个二维网格中。位于这个<tt>sizer</tt>的孩子列表中的第一个窗口部件放置在网格的左上角，其余的按从左到右，从上到下的方式排列，直到最后一个窗口部件被放置在网格的右底部。图11.1显示了一个例子，有九个窗口部件被放置在一个3*3的网格中。注意每个部件之间有一些间隙。 <span id="line-84" class="anchor"></span><span id="line-85" class="anchor"></span></p>
<p class="line867"><strong>图11.1</strong> <span id="line-86" class="anchor"></span><span id="line-87" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.1.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.1.gif" class="attachment"><span id="line-88" class="anchor"></span><span id="line-89" class="anchor"></span></p>
<p class="line862">当你调整<tt>grid</tt> <tt>sizer</tt>的大小时，每个部件之间的间隙将随之改变，但是默认情况下，窗口部件的尺寸不会变，并且始终按左上角依次排列。图11.2显示了调整尺寸后的同一窗口。 <span id="line-90" class="anchor"></span><span id="line-91" class="anchor"></span></p>
<p class="line867"><strong>图11.2</strong> <span id="line-92" class="anchor"></span><span id="line-93" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.2.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.2.gif" class="attachment"><span id="line-94" class="anchor"></span><span id="line-95" class="anchor"></span></p>
<p class="line874">例11.2显示了用于产生图11.1和11.2的代码。 <span id="line-96" class="anchor"></span><span id="line-97" class="anchor"></span></p>
<p class="line867"><strong>例11.2</strong>
<strong>使用<tt>grid</tt></strong>  <strong><tt>sizer</tt></strong> <span id="line-98" class="anchor"></span><span id="line-99" class="anchor"></span></p>
<p class="line867"><span id="line-100" class="anchor"></span><span id="line-101" class="anchor"></span><span id="line-102" class="anchor"></span><span id="line-103" class="anchor"></span><span id="line-104" class="anchor"></span><span id="line-105" class="anchor"></span><span id="line-106" class="anchor"></span><span id="line-107" class="anchor"></span><span id="line-108" class="anchor"></span><span id="line-109" class="anchor"></span><span id="line-110" class="anchor"></span><span id="line-111" class="anchor"></span><span id="line-112" class="anchor"></span><span id="line-113" class="anchor"></span><span id="line-114" class="anchor"></span><span id="line-115" class="anchor"></span><span id="line-116" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-03941c1035719e37f7a434a67e3b064932ac3bbc_001\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-03941c1035719e37f7a434a67e3b064932ac3bbc_001', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-03941c1035719e37f7a434a67e3b064932ac3bbc_001" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">GridSizerFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Basic Grid Sizer"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">GridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">)</span><span class="Comment">#创建grid sizer</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   9 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span><span class="Comment">#添加窗口部件到sizer</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span><span class="Comment">#把sizer与框架关联起来</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span><span class="ID">GridSizerFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-117" class="anchor"></span><p class="line862">你可以从例11.2看到，一个<tt>grid</tt> <tt>sizer</tt>是类<tt>wx.GridSizer</tt>的一个实例。构造函数显式地设置四个属性，这些属性是<tt>grid</tt> <tt>sizer</tt>独一无二的： <span id="line-118" class="anchor"></span><span id="line-119" class="anchor"></span></p>
<p class="line867"><span id="line-120" class="anchor"></span><span id="line-121" class="anchor"></span><span id="line-122" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-60c69a321af10a457617efb18a65f88e07a9b8db_002\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-60c69a321af10a457617efb18a65f88e07a9b8db_002', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-60c69a321af10a457617efb18a65f88e07a9b8db_002" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">GridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-123" class="anchor"></span><p class="line862">这个构造函数中的<tt>rows</tt>和<tt>cols</tt>是整数，它们指定了网格的尺寸——所能放置的窗口部件的数量。如果这两个参数之一被设置为0，那么它的实际的值根据<tt>sizer</tt>中的孩子的数量而定。例如如果使用<tt>wx.GridSizer(2</tt>, 0, 0, 0)，且<tt>sizer</tt>有八个孩子，那么它就需要有四列来填充这些孩子。 <span id="line-124" class="anchor"></span><span id="line-125" class="anchor"></span></p>
<p class="line867"><tt>vgap</tt>和<tt>hgap</tt>使你可以决定窗口控件间的间隔的多少。<tt>vgap</tt>是两相邻列间的间隔的象素量，<tt>hgapvgap</tt>是两相邻行间的间隔的象素量。这些象素量是除了窗口控件边框的量。属性<tt>rows</tt>, <tt>cols</tt>, <tt>vgap</tt>, <tt>hgap</tt>都有各自的<tt>get</tt>*和<tt>set</tt>*方法——<tt>GetRows()</tt>, <tt>SetRows(rows)</tt>, <tt>GetCols()</tt>,<tt>SetCols(cols)</tt>, <tt>GetVGap()</tt>, <tt>SetVGap(gap)</tt>, <tt>GetHGap()</tt>, 和<tt>SetHGap(gap)</tt> 。 <span id="line-126" class="anchor"></span><span id="line-127" class="anchor"></span></p>
<p class="line867"><tt>grid</tt> <tt>sizer</tt>的尺寸和位置的算法是十分简单的。当<tt>Fit()</tt>第一次被调用时，创建初始化的网格布局。如果有必要，行和列的数量根据列表中元素的数量来计算。在<tt>grid</tt>中每个空格的尺寸是相同的——即使每个窗口部件的尺寸不同。这个最大的尺度是根据网格中宽度最宽的孩子的宽度和高度最高的孩子的高度来计算的。所以，<tt>grid</tt> <tt>sizer</tt>最适合用于所有孩子相同尺寸的情况。有着不同尺寸窗口部件的<tt>grid</tt> <tt>sizer</tt>看起来有点怪异。如果你仍想要一个类似<tt>grid</tt>的布局，但是你又有不同尺寸的窗口部件的话，那么可以使用<tt>flex</tt>
<tt>grid</tt> <tt>sizer</tt>或<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>。 <span id="line-128" class="anchor"></span><span id="line-129" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-efdb330edd8b6cd2d0488f8dba1a59362c7778c4">1.2.2. 如何对sizer添加或移除孩子？</h3>
<span id="line-130" class="anchor"></span><p class="line862">添加孩子部件到<tt>sizer</tt>中的次序是非常重要的。这与将孩子添加到一个父窗口部件中的通常情况是不一样的。<tt>sizer</tt>的通常的布局算法要求一次添加一个孩子，以便于决定它们的显示位置。下一项的位置是依赖于前一被添加项的位置的。例如，<tt>grid</tt> <tt>sizer</tt>基于窗口部件的次序来从左到右，从上到下的添加并显示。在多数情况下，当你在父窗口部件的构造器中创建<tt>sizer</tt>时，你将会按正确的次序添加这些项目。但是有时候，如果你在运行时动态地改变你的布局，那么你需要更灵活和更细致。 <span id="line-131" class="anchor"></span><span id="line-132" class="anchor"></span></p>
<p class="line867"><strong>使用<tt>Add()</tt>方法</strong> <span id="line-133" class="anchor"></span><span id="line-134" class="anchor"></span></p>
<p class="line862">添加一个窗口部件到一个<tt>sizer</tt>中的最常用的方法是<tt>Add()</tt>，它将新的窗口部件添加到<tt>sizer</tt>的孩子列表的尾部。“添加到<tt>sizer</tt>的孩子列表的尾部”的准确的意思信赖于该<tt>sizer</tt>的类型，但是通常它意味这个新的窗口部件将依次显示在右下位置。<tt>Add()</tt>方法有三个不同的样式： <span id="line-135" class="anchor"></span><span id="line-136" class="anchor"></span></p>
<p class="line867"><span id="line-137" class="anchor"></span><span id="line-138" class="anchor"></span><span id="line-139" class="anchor"></span><span id="line-140" class="anchor"></span><span id="line-141" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-a1898707080936910dc91d6e8a5964aae7aff28b_003\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-a1898707080936910dc91d6e8a5964aae7aff28b_003', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-a1898707080936910dc91d6e8a5964aae7aff28b_003" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">window</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">size</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-142" class="anchor"></span><p class="line862">第一个版本是你最常要用到的，它使你能够将一个窗口部件添加到<tt>sizer</tt>。 <span id="line-143" class="anchor"></span><span id="line-144" class="anchor"></span></p>
<p class="line862">第二个版本用于将一个<tt>sizer</tt>嵌套在另一个中——这最常用于<tt>box</tt> <tt>sizer</tt>，但可用于任何类型的<tt>sizer</tt>。 <span id="line-145" class="anchor"></span><span id="line-146" class="anchor"></span></p>
<p class="line862">第三个版本使你能够添加一个<tt>wx.Size</tt>对象的空的空白尺寸或一个（宽，高）元组到<tt>sizer</tt>，通常用作一个分隔符（例如，在一个工具栏中）。另外，这在<tt>box</tt> <tt>sizer</tt>中最常使用，但也可在任何<tt>sizer</tt>中使用以用于形成窗口的一个空白区域或分隔不同的窗口部件。 <span id="line-147" class="anchor"></span><span id="line-148" class="anchor"></span></p>
<p class="line862">其它的参数影响<tt>sizer</tt>中的项目如何显示。其中的一些只对某种<tt>sizer</tt>有效。<tt>proportion</tt>仅被<tt>box</tt>
<tt>sizer</tt>使用，并当父窗口尺寸改变时影响一个项目如何被绘制。这个稍后我们将在<tt>box</tt> <tt>sizer</tt>时讨论。 <span id="line-149" class="anchor"></span><span id="line-150" class="anchor"></span></p>
<p class="line867"><tt>flag</tt>参数用于放置位标记，它控制对齐、边框和调整尺寸。这些项将在后面的章节中讨论。如果在<tt>flag</tt>参数中指定了边框，那么<tt>border</tt>参数包含边框的宽度。如果<tt>sizer</tt>的算法需要，<tt>userData</tt>参数可被用来传递额外的数据。如果你正在设计一个自定义的<tt>sizer</tt>，那么你可以使用该参数。 <span id="line-151" class="anchor"></span><span id="line-152" class="anchor"></span></p>
<p class="line867"><strong>使用<tt>insert()</tt>方法</strong> <span id="line-153" class="anchor"></span><span id="line-154" class="anchor"></span></p>
<p class="line862">这里有用于将新的窗口部件插入到<tt>sizer</tt>中不同位置的方法。<tt>insert()</tt>方法使你能够按任意的索引来放置新的窗口部件。它也有三个形式： <span id="line-155" class="anchor"></span><span id="line-156" class="anchor"></span></p>
<p class="line867"><span id="line-157" class="anchor"></span><span id="line-158" class="anchor"></span><span id="line-159" class="anchor"></span><span id="line-160" class="anchor"></span><span id="line-161" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-cb8cb40ac1fa9f65803af194e5de316850652439_004\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-cb8cb40ac1fa9f65803af194e5de316850652439_004', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-cb8cb40ac1fa9f65803af194e5de316850652439_004" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">Insert</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">window</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">Insert</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">sizer</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">Insert</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-162" class="anchor"></span><p class="line867"><strong>使用<tt>Prepend()</tt>方法</strong> <span id="line-163" class="anchor"></span><span id="line-164" class="anchor"></span></p>
<p class="line862">该方法将新的窗口部件、<tt>sizer</tt>或空白添加到<tt>sizer</tt>的列表的开头，这意味所添加的东西将被显示到左上角： <span id="line-165" class="anchor"></span><span id="line-166" class="anchor"></span></p>
<p class="line867"><span id="line-167" class="anchor"></span><span id="line-168" class="anchor"></span><span id="line-169" class="anchor"></span><span id="line-170" class="anchor"></span><span id="line-171" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-3820ae436d1fc972966a1ba991cf73b946b4673c_005\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-3820ae436d1fc972966a1ba991cf73b946b4673c_005', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-3820ae436d1fc972966a1ba991cf73b946b4673c_005" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">Prepend</span><span class="Operator">(</span><span class="ID">window</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">Prepend</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">Prepend</span><span class="Operator">(</span><span class="ID">size</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">border</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">userData</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-172" class="anchor"></span><p class="line862">图11.3显示了在例11.1中如果<tt>Add()</tt>替换为<tt>Prepend()</tt>后的布局。 <span id="line-173" class="anchor"></span><span id="line-174" class="anchor"></span></p>
<p class="line867"><strong>图11.3</strong> <span id="line-175" class="anchor"></span><span id="line-176" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.3.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.3.gif" class="attachment"><span id="line-177" class="anchor"></span><span id="line-178" class="anchor"></span></p>
<p class="line862">如果<tt>sizer</tt>已在屏幕上显示了，而你又要给<tt>sizer</tt>添加一个新的项目，那么你需要调用<tt>sizer</tt>的<tt>Layout()</tt>方法来迫使<tt>sizer</tt>自己重新排列，以容纳新的项。 <span id="line-179" class="anchor"></span><span id="line-180" class="anchor"></span></p>
<p class="line867"><strong>使用<tt>Detach()</tt>方法</strong> <span id="line-181" class="anchor"></span><span id="line-182" class="anchor"></span></p>
<p class="line862">为了从<tt>sizer</tt>中移除一项，你需要调用<tt>Detach()</tt>方法，它从<tt>sizer</tt>中移除项目，但是没有销毁该项目。这对于你以后再使用它是有用的。使用<tt>Detach()</tt>有三种方法。你可以将你想要移除的窗口、<tt>sizer</tt>对象、对象的索引作为参数传递给<tt>Detach()</tt>： <span id="line-183" class="anchor"></span><span id="line-184" class="anchor"></span></p>
<p class="line867"><span id="line-185" class="anchor"></span><span id="line-186" class="anchor"></span><span id="line-187" class="anchor"></span><span id="line-188" class="anchor"></span><span id="line-189" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-552f35951e042557c98cfa4506ce8ae543c3d736_006\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-552f35951e042557c98cfa4506ce8ae543c3d736_006', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-552f35951e042557c98cfa4506ce8ae543c3d736_006" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">Detach</span><span class="Operator">(</span><span class="ID">window</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">Detach</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">Detach</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-190" class="anchor"></span><p class="line862">在这三种情况中，<tt>Detach()</tt>方法返回一个布尔值，它表明项目是否真的被删除了——如果你试图移除<tt>sizer</tt>中没有的项，将返回<tt>false</tt>。和你曾见过的其它的删除方法不同，<tt>Detach()</tt>不返回被删除的项目，所以如果你想要得到它的话，你需要之前用一个变量来存储对它的引用。 <span id="line-191" class="anchor"></span><span id="line-192" class="anchor"></span></p>
<p class="line862">从<tt>sizer</tt>中删除项目，不会自动改变在屏幕上的显示。你需要调用<tt>Layout()</tt>方法来执行重绘。 <span id="line-193" class="anchor"></span><span id="line-194" class="anchor"></span></p>
<p class="line862">你可以得到一个包含了窗口的<tt>sizer</tt>的引用，这通过使用<tt>wx.Window</tt>的<tt>GetContainingSizer()</tt>方法。如果该窗口部件没有被包含在<tt>sizer</tt>中，那么该方法返回<tt>None</tt>。 <span id="line-195" class="anchor"></span><span id="line-196" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-425f88a0912efe2e3ba3413df4c152c5ab7e649b">1.2.3. sizer是如何管理它的孩子的尺寸和对齐的？</h3>
<span id="line-197" class="anchor"></span><p class="line862">当一个新的项目被添加到一个<tt>sizer</tt>时，<tt>sizer</tt>就使用这个项目的初始尺寸或根据它的布局计算给出恰当的尺寸（如果它的初始尺寸没有设置）。换句话说，<tt>sizer</tt>不调整一个项目的大小，除非要求，这通常发生在一个窗口尺寸的改变时。 <span id="line-198" class="anchor"></span><span id="line-199" class="anchor"></span></p>
<p class="line862">当<tt>sizer</tt>的父窗口部件改变了尺寸时，<tt>sizer</tt>需要改变它的组分的尺寸。默认情况下，<tt>sizer</tt>保持这些窗口部件的对齐方式不变。 <span id="line-200" class="anchor"></span><span id="line-201" class="anchor"></span></p>
<p class="line862">当你添加一个窗口部件到<tt>sizer</tt>时，可以通过给<tt>flag</tt>参数一个特定值来调整该窗口部件的尺寸改变行为。图11.4展示了在用户放大窗口后，几个不同标记应用于这个基本的<tt>grid</tt> <tt>sizer</tt>的结果。 <span id="line-202" class="anchor"></span><span id="line-203" class="anchor"></span></p>
<p class="line867"><strong>图11.4</strong> <span id="line-204" class="anchor"></span><span id="line-205" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.4.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.4.gif" class="attachment"><span id="line-206" class="anchor"></span><span id="line-207" class="anchor"></span></p>
<p class="line862">例11.3显示了产生图11.4的代码。除了在窗口部件被添加到<tt>sizer</tt>时应用了一个标记字典外，其它的与前一个例子相同。 <span id="line-208" class="anchor"></span><span id="line-209" class="anchor"></span></p>
<p class="line867"><strong>例11.3 使用了用于对齐和调整尺寸的标记的一个<tt>grid</tt> <tt>sizer</tt></strong> <span id="line-210" class="anchor"></span><span id="line-211" class="anchor"></span></p>
<p class="line867"><span id="line-212" class="anchor"></span><span id="line-213" class="anchor"></span><span id="line-214" class="anchor"></span><span id="line-215" class="anchor"></span><span id="line-216" class="anchor"></span><span id="line-217" class="anchor"></span><span id="line-218" class="anchor"></span><span id="line-219" class="anchor"></span><span id="line-220" class="anchor"></span><span id="line-221" class="anchor"></span><span id="line-222" class="anchor"></span><span id="line-223" class="anchor"></span><span id="line-224" class="anchor"></span><span id="line-225" class="anchor"></span><span id="line-226" class="anchor"></span><span id="line-227" class="anchor"></span><span id="line-228" class="anchor"></span><span id="line-229" class="anchor"></span><span id="line-230" class="anchor"></span><span id="line-231" class="anchor"></span><span id="line-232" class="anchor"></span><span id="line-233" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-5fd1a01356ea1bde9b76d50a65330a7dabf27a2d_007\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-5fd1a01356ea1bde9b76d50a65330a7dabf27a2d_007', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-5fd1a01356ea1bde9b76d50a65330a7dabf27a2d_007" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="Comment">#对齐标记</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="Comment"></span><span class="ID">flags</span> <span class="Operator">=</span> <span class="Operator">{</span><span class="String">"one"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALIGN_BOTTOM</span><span class="Operator">,</span> <span class="String">"two"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALIGN_CENTER</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   6 </span>         <span class="String">"four"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALIGN_RIGHT</span><span class="Operator">,</span> <span class="String">"six"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">,</span> <span class="String">"seven"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   7 </span>         <span class="String">"eight"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">SHAPED</span><span class="Operator">}</span></span>
<span class="line"><span class="LineNumber">   8 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   9 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"GridSizer
Resizing"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">GridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  13 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>            <span class="ID">flag</span> <span class="Operator">=</span> <span class="ID">flags</span><span class="Operator">.</span><span class="ID">get</span><span class="Operator">(</span><span class="ID">label</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span><span class="ID">TestFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-234" class="anchor"></span><p class="line862">在这个例子中，窗口部件“<tt>one</tt>,” “<tt>two</tt>,” 和“<tt>four</tt>”分别使用<tt>wx.ALIGN_BOTTOM</tt>, <tt>wx.ALIGN_CENTER</tt>, <tt>and</tt>
<tt>wx.ALIGN_RIGHT</tt>标记改变它们的对齐方式。当窗口大小改变时，你可以看到效果，部件“<tt>three</tt>"没有指定一个标记，所以它按左上角对齐。窗口"<tt>six</tt>"和"<tt>seven</tt>"均使用了<tt>wx.EXPAND</tt>标记来告诉<tt>sizer</tt>改变它们的尺寸以填满格子，而窗口部件"<tt>eight</tt>"使用<tt>wx.SHAPED</tt>来改变它的尺寸，以保持比例不变。 <span id="line-235" class="anchor"></span><span id="line-236" class="anchor"></span></p>
<p class="line862">表11.2显示与尺寸调整和对齐相关的<tt>flag</tt>的值。 <span id="line-237" class="anchor"></span><span id="line-238" class="anchor"></span></p>
<p class="line867"><strong>表11.2 尺寸调整和对齐行为标记</strong> <span id="line-239" class="anchor"></span></p>
<div><table><tbody>
<tr>
<td><p class="line891"><tt>wx.ALIGN_BOTTOM</tt> </p></td>
<td><p class="line862">按照窗口部件被分配的空间（格子）的底部对齐。 </p></td>
</tr>
<tr>
<td>
<span id="line-240" class="anchor"></span><p class="line891"><tt>wx.ALIGN_CENTER</tt> </p>
</td>
<td><p class="line862">放置窗口部件，使窗口部件的中心处于其所分配的空间的中心。 </p></td>
</tr>
<tr>
<td>
<span id="line-241" class="anchor"></span><p class="line891"><tt>wx.ALIGN_CENTER_HORIZONTAL</tt> </p>
</td>
<td><p class="line862">在它所处的格子中，水平居中。 </p></td>
</tr>
<tr>
<td>
<span id="line-242" class="anchor"></span><p class="line891"><tt>wx.ALIGN_CENTER_VERTICAL</tt> </p>
</td>
<td><p class="line862">在它所处的格子中，垂直居中。 </p></td>
</tr>
<tr>
<td>
<span id="line-243" class="anchor"></span><p class="line891"><tt>wx.ALIGN_LEFT</tt> </p>
</td>
<td><p class="line862">靠着它所处的格子左边缘。这是默认行为。 </p></td>
</tr>
<tr>
<td>
<span id="line-244" class="anchor"></span><p class="line891"><tt>wx.ALIGN_TOP</tt> </p>
</td>
<td><p class="line862">靠着它所处的格子的上边缘。这是默认的行为。 </p></td>
</tr>
<tr>
<td>
<span id="line-245" class="anchor"></span><p class="line891"><tt>wx.EXPAND</tt> </p>
</td>
<td><p class="line862">填满它所处的格子空间。 </p></td>
</tr>
<tr>
<td>
<span id="line-246" class="anchor"></span><p class="line891"><tt>wx.FIXED_MINSIZE</tt> </p>
</td>
<td><p class="line862">保持固定项的最小尺寸。 </p></td>
</tr>
<tr>
<td>
<span id="line-247" class="anchor"></span><p class="line891"><tt>wx.GROW</tt> </p>
</td>
<td><p class="line862">与<tt>wx.EXPAND</tt>相同。但比之少两个字符，节约了时间。 </p></td>
</tr>
<tr>
<td>
<span id="line-248" class="anchor"></span><p class="line891"><tt>wx.SHAPED</tt> </p>
</td>
<td><p class="line862">窗口部件的尺寸改变时，只在一个方向上填满格子，另一个方向上按窗口部件原先的形状尺寸的比列填充。 </p></td>
</tr>
</tbody></table></div>
<span id="line-249" class="anchor"></span><span id="line-250" class="anchor"></span><span id="line-251" class="anchor"></span><p class="line862">这些标记可以使用|来组合，有时，这些组合会很有意思。<tt>wx.ALIGN_TOP</tt> | <tt>wx.ALIGN_RIGHT</tt>使得窗口部件位于格子的右上角。（注意，互相排斥的标记组合如<tt>wx.ALIGN_TOP</tt> | <tt>wx.ALIGN_BOTTOM</tt>中，默认的标记不起作用，这是因为默认标记的相应位上是0，在或操作中没有什么影响）。 <span id="line-252" class="anchor"></span><span id="line-253" class="anchor"></span></p>
<p class="line862">还有一些方法，你可以用来在运行时处理<tt>sizer</tt>或它的孩子的尺寸和位置。你可以使用方法<tt>GetSize()</tt>和 <tt>GetPosition()</tt>来得到<tt>sizer</tt>的当前尺寸和位置——这个位置是<tt>sizer</tt>相对于它所关联的容器的。如果<tt>sizer</tt>嵌套在另一个<tt>sizer</tt>中，那么这些方法是很有用的。你可以通过调用<tt>SetDimension(x</tt>, y, <tt>width</tt>,
<tt>height)</tt>方法来指定一个<tt>sizer</tt>的尺寸，这样<tt>sizer</tt>将根据它的新尺寸和位置重新计算它的孩子的尺寸。 <span id="line-254" class="anchor"></span><span id="line-255" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-95ff96bce5a752f8ec342c24259e635e8f335f5c">1.2.4. 能够为sizer或它的孩子指定一个最小的尺寸吗？</h3>
<span id="line-256" class="anchor"></span><p class="line867"><tt>sizer</tt>的窗口部件的布局中的另一个重要的要素是为<tt>sizer</tt>或它的孩子指定一个最小尺寸的能力。一般你不想要一个控件或一个<tt>sizer</tt>小于一个特定的尺寸，通常因为这样会导致文本被窗口部件的边缘截断。或在一个嵌套的<tt>sizer</tt>中，控件在窗口中不能被显示出来。为了避免诸如此类的情况，你可以使用最小尺寸。 <span id="line-257" class="anchor"></span><span id="line-258" class="anchor"></span></p>
<p class="line874">图11.5显示了对一个特定的窗口部件设置最小尺寸的一个例子。该窗口的尺寸已被用户改变了。 <span id="line-259" class="anchor"></span><span id="line-260" class="anchor"></span></p>
<p class="line867"><strong>图11.5</strong> <span id="line-261" class="anchor"></span><span id="line-262" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.5.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.5.gif" class="attachment"><span id="line-263" class="anchor"></span><span id="line-264" class="anchor"></span></p>
<p class="line862">例11.4展示了产生该图的代码。它类似于基本的<tt>grid</tt>的代码，其中增加了一个<tt>SetMinSize()</tt>调用。 <span id="line-265" class="anchor"></span><span id="line-266" class="anchor"></span></p>
<p class="line867"><strong>例11.4</strong>  <strong>使用最小尺寸设置的<tt>grid</tt></strong>  <strong><tt>sizer</tt></strong> <span id="line-267" class="anchor"></span><span id="line-268" class="anchor"></span></p>
<p class="line867"><span id="line-269" class="anchor"></span><span id="line-270" class="anchor"></span><span id="line-271" class="anchor"></span><span id="line-272" class="anchor"></span><span id="line-273" class="anchor"></span><span id="line-274" class="anchor"></span><span id="line-275" class="anchor"></span><span id="line-276" class="anchor"></span><span id="line-277" class="anchor"></span><span id="line-278" class="anchor"></span><span id="line-279" class="anchor"></span><span id="line-280" class="anchor"></span><span id="line-281" class="anchor"></span><span id="line-282" class="anchor"></span><span id="line-283" class="anchor"></span><span id="line-284" class="anchor"></span><span id="line-285" class="anchor"></span><span id="line-286" class="anchor"></span><span id="line-287" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-bbca342a82455ba26dd396250bbc3cfd73b5f471_008\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-bbca342a82455ba26dd396250bbc3cfd73b5f471_008', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-bbca342a82455ba26dd396250bbc3cfd73b5f471_008" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span>
<span class="String">"GridSizer Test"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">GridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   9 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">center</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">FindWindowByName</span><span class="Operator">(</span><span class="String">"five"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">center</span><span class="Operator">.</span><span class="ID">SetMinSize</span><span class="Operator">(</span><span class="Operator">(</span><span class="Number">150</span><span class="Operator">,</span><span class="Number">50</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span><span class="ID">TestFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-288" class="anchor"></span><p class="line862">当一个<tt>sizer</tt>被创建时，它根据它的孩子的综合的最小尺寸（最小的宽度和最小的高度）隐含地创建一个最小尺寸。多数控件都知道它们最小化的“最佳尺寸”，<tt>sizer</tt>查询该值以确定默认的布局。如果显式地使用一个尺寸值来创建一个控件，那么这个设置的尺寸值覆盖所计算出的最佳尺寸。一个控件的最小尺寸可以使用窗口的方法<tt>SetMinSize(width</tt>, <tt>height)</tt>和<tt>SetSizeHints(minW</tt>,
<tt>minH</tt>, <tt>maxW</tt>, <tt>maxH)</tt>来设置——第二个方法使你也能够指定一个最大尺寸。如果一个窗口部件的属性（通常是所显示的字体或文本标签）改变，该窗口部件通常将调整它的最佳尺寸。 <span id="line-289" class="anchor"></span><span id="line-290" class="anchor"></span></p>
<p class="line862">如果一个窗口有相关的<tt>sizer</tt>，那么这个容器窗口的最佳尺寸由它的<tt>sizer</tt>来确定。如果没有，那么该窗口的最佳尺寸就是足够大到显示所有子控件的尺寸。如果该窗口没有孩子，那么它使用所设置的最小尺寸为最佳尺寸。如果上述都没有，那么该容器窗口的当前尺寸作为其最佳尺寸。 <span id="line-291" class="anchor"></span><span id="line-292" class="anchor"></span></p>
<p class="line862">你可以使用<tt>GetMinSize()</tt>来访问整个<tt>sizer</tt>的最小尺寸。如果你想为整个<tt>sizer</tt>设置一个较大的最小尺寸，那么你可以使用<tt>SetMinSize(width</tt>,<tt>height)</tt>，该函数也可以使用一个<tt>wx.Size</tt>实例作为参数——<tt>SetMinSize(size)</tt>，尽管在<tt>wxPython</tt>中你很少显式地创建一个<tt>wx.Size</tt>。在最小尺寸已被设置后，<tt>GetMinSize()</tt>返回设置的尺寸或孩子的综合的尺寸。 <span id="line-293" class="anchor"></span><span id="line-294" class="anchor"></span></p>
<p class="line862">如果你只想设置<tt>sizer</tt>内的一个特定的孩子的最小尺寸，那么使用<tt>sizer</tt>的<tt>SetItemMinSize()</tt>方法。它也有三个形式： <span id="line-295" class="anchor"></span><span id="line-296" class="anchor"></span></p>
<p class="line867"><span id="line-297" class="anchor"></span><span id="line-298" class="anchor"></span><span id="line-299" class="anchor"></span><span id="line-300" class="anchor"></span><span id="line-301" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-fcbadfeafab6c4b99a9c52b0e32ca9991573f77e_009\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-fcbadfeafab6c4b99a9c52b0e32ca9991573f77e_009', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-fcbadfeafab6c4b99a9c52b0e32ca9991573f77e_009" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">SetItemMinSize</span><span class="Operator">(</span><span class="ID">window</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">SetItemMinSize</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">SetItemMinSize</span><span class="Operator">(</span><span class="ID">index</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-302" class="anchor"></span><p class="line862">这里，参数<tt>window</tt>和<tt>sizer</tt>必须是一个<tt>sizer</tt>实例的孩子。如果需要的话，方法将在整个嵌套树中搜索特定的子窗口或子<tt>sizer</tt>。参数<tt>index</tt>是<tt>sizer</tt>的孩子列表中的索引。参数<tt>size</tt>是一个<tt>wx.Size</tt>对象或一个(宽，高)元组，它是<tt>sizer</tt>中的项目被设置的最小尺寸。如果你设置的最小尺寸比窗口部件当前的尺寸大，那么它自动调整。你不能根据<tt>sizer</tt>来设置最大尺寸，只能根据窗口部件来使用<tt>SetSizeHints()</tt>设置。
<span id="line-303" class="anchor"></span><span id="line-304" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-b2f340a3d187a3c74bea1e712be145adfa0905ef">1.2.5. sizer如何管理每个孩子的边框？</h3>
<span id="line-305" class="anchor"></span><p class="line867"><tt>wxPython</tt> <tt>sizer</tt>能够使它的一个或所有孩子有一个边框。边框是连续数量的空白空间，它们分离相邻的窗口部件。当<tt>sizer</tt>计算它的孩子的布置时，边框的尺寸是被考虑进去了的，孩子的尺寸不会小于边框的宽度。当<tt>sizer</tt>调整尺寸时，边框的尺寸不会改变。 <span id="line-306" class="anchor"></span><span id="line-307" class="anchor"></span></p>
<p class="line874">图11.6显示了一个10像素的边框。在每行中，中间的元素四边都有边框围绕，而其它的只是部分边有边框。增加边框不会使窗口部件更小，而是使得框架更大了。 <span id="line-308" class="anchor"></span><span id="line-309" class="anchor"></span></p>
<p class="line867"><strong>图11.6</strong> <span id="line-310" class="anchor"></span><span id="line-311" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.6.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.6.gif" class="attachment"><span id="line-312" class="anchor"></span><span id="line-313" class="anchor"></span></p>
<p class="line862">例11.5是产生图11.6的相关代码。它和基本的<tt>grid</tt> <tt>sizer</tt>相似，只是我们增加了一个边框值字典，并给<tt>Add()</tt>一个10像素的边框。 <span id="line-314" class="anchor"></span><span id="line-315" class="anchor"></span></p>
<p class="line867"><strong>例11.5</strong>  <strong>使用边框设置的<tt>grid</tt></strong>  <strong><tt>sizer</tt>代码</strong> <span id="line-316" class="anchor"></span><span id="line-317" class="anchor"></span></p>
<p class="line867"><span id="line-318" class="anchor"></span><span id="line-319" class="anchor"></span><span id="line-320" class="anchor"></span><span id="line-321" class="anchor"></span><span id="line-322" class="anchor"></span><span id="line-323" class="anchor"></span><span id="line-324" class="anchor"></span><span id="line-325" class="anchor"></span><span id="line-326" class="anchor"></span><span id="line-327" class="anchor"></span><span id="line-328" class="anchor"></span><span id="line-329" class="anchor"></span><span id="line-330" class="anchor"></span><span id="line-331" class="anchor"></span><span id="line-332" class="anchor"></span><span id="line-333" class="anchor"></span><span id="line-334" class="anchor"></span><span id="line-335" class="anchor"></span><span id="line-336" class="anchor"></span><span id="line-337" class="anchor"></span><span id="line-338" class="anchor"></span><span id="line-339" class="anchor"></span><span id="line-340" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-3ce287dddfeeed0a4f2579fbd75d6027525dd2ec_010\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-3ce287dddfeeed0a4f2579fbd75d6027525dd2ec_010', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-3ce287dddfeeed0a4f2579fbd75d6027525dd2ec_010" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="Comment">#边框标记</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="Comment"></span><span class="ID">flags</span> <span class="Operator">=</span> <span class="Operator">{</span><span class="String">"one"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BOTTOM</span><span class="Operator">,</span> <span class="String">"two"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="String">"three"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TOP</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   6 </span>         <span class="String">"four"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LEFT</span><span class="Operator">,</span> <span class="String">"five"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="String">"six"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">RIGHT</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   7 </span>         <span class="String">"seven"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BOTTOM</span> <span class="Operator">|</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TOP</span><span class="Operator">,</span> <span class="String">"eight"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   8 </span>         <span class="String">"nine"</span><span class="Operator">:</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">LEFT</span> <span class="Operator">|</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">RIGHT</span><span class="Operator">}</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  10 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"GridSizer Borders"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">GridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  14 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>            <span class="ID">flag</span> <span class="Operator">=</span> <span class="ID">flags</span><span class="Operator">.</span><span class="ID">get</span><span class="Operator">(</span><span class="ID">label</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">,</span> <span class="Number">10</span><span class="Operator">)</span><span class="Comment">#添加指定边框的窗口部件</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span><span class="ID">TestFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-341" class="anchor"></span><p class="line862">要在一个<tt>sizer</tt>中的窗口部件周围放置边框，需要两步。第一步是当窗口部件被添加到该<tt>sizer</tt>时，传递额外的标记给<tt>flags</tt>参数。你可以使用标记<tt>wx.ALL</tt>来指定边框围绕整个窗口部件，或使用<tt>wx.BOTTOM</tt>, <tt>wx.LEFT</tt>, <tt>wx.RIGHT</tt>, <tt>wx.TOP</tt>来指定某一边有边框。这些标记当然可以组合成你想要的，如<tt>wx.RIGHT</tt> |
<tt>wx.BOTTOM</tt>将使你的窗口部件的右边和底边有边框。由于边框、尺寸调整、对齐这些信息都是经由<tt>flags</tt>参数，所以对于同一个窗口部件，你通常必须将三种标记组合起来使用。 <span id="line-342" class="anchor"></span><span id="line-343" class="anchor"></span></p>
<p class="line862">在你传递了边框信息到<tt>flags</tt>参数后，你也需要传递边框宽度的像素值给<tt>border</tt>参数。例如，下面的调用将添加窗口部件到<tt>sizer</tt>列表的尾部，并使该窗口部件的周围有5个像素宽度的边框： <span id="line-344" class="anchor"></span><span id="line-345" class="anchor"></span></p>
<p class="line867"><span id="line-346" class="anchor"></span><span id="line-347" class="anchor"></span><span id="line-348" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-59fb9804dbf52b2b16a8214ea941f4e07bfaeba1_011\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-59fb9804dbf52b2b16a8214ea941f4e07bfaeba1_011', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-59fb9804dbf52b2b16a8214ea941f4e07bfaeba1_011" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">widget</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span> <span class="Operator">|</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-349" class="anchor"></span><p class="line874">该部件然后将扩展以填充它的有效空间，且四周始终留有5个像素的空白。 <span id="line-350" class="anchor"></span><span id="line-351" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-ae9b93628558f04bdbc27a01d4b17fb924e883d2">1.3. 使用其它类型的sizer</h2>
<span id="line-352" class="anchor"></span><p class="line862">我们已经讨论了基本的<tt>sizer</tt>，现在我们可以转向更复杂和更灵活的<tt>sizer</tt>了。其中两个（<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>和<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>）本质上是<tt>grid</tt>的变种。另外两个（<tt>box</tt>和<tt>static</tt> <tt>box</tt> <tt>sizer</tt>）使用一个不同的和更灵活的布局结构。 <span id="line-353" class="anchor"></span><span id="line-354" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-1bfb417861035f00bd754d8ab441cbe05079f952">1.3.1. 什么是flex grid  sizer？</h3>
<span id="line-355" class="anchor"></span><p class="line867"><tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>是<tt>grid</tt> <tt>sizer</tt>的一个更灵活的版本。它与标准的<tt>grid</tt> <tt>sizer</tt>几乎相同，除了下面的例外： <span id="line-356" class="anchor"></span><span id="line-357" class="anchor"></span></p>
<p class="line874">1、每行和每列可以有各自的尺寸。 <span id="line-358" class="anchor"></span><span id="line-359" class="anchor"></span></p>
<p class="line874">2、默认情况下，当尺寸调整时，它不改变它的单元格的尺寸。如果需要的话，你可以指定哪行或哪列应该增长。 <span id="line-360" class="anchor"></span><span id="line-361" class="anchor"></span></p>
<p class="line874">3、它可以在两个方向之一灵活地增长，意思是你可以为个别的子元素指定比列量，并且你可以指定固定方向上的行为。 <span id="line-362" class="anchor"></span><span id="line-363" class="anchor"></span></p>
<p class="line862">图11.7显示了一个<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>，它的布局也是9个单元格。这里的中间单元格更大。 <span id="line-364" class="anchor"></span><span id="line-365" class="anchor"></span></p>
<p class="line867"><strong>图11.7</strong> <span id="line-366" class="anchor"></span><span id="line-367" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.7.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.7.gif" class="attachment"><span id="line-368" class="anchor"></span><span id="line-369" class="anchor"></span></p>
<p class="line862">与图11.5相比较，对于相同的布局，图11.5中每个单元格的尺寸与中间对象的相同，在<tt>flex</tt> <tt>grid</tt>
<tt>sizer</tt>中，单元格的尺寸大小根据它所在的行和列来定。它们宽度是该列中宽度最大的项目的宽度，它们的高度是该行中宽度最高的项目的宽度。在这里，项目“<tt>four</tt>”和项目“<tt>six</tt>”的单元格的高度比项目本身的高度更高，因为其同行中的项目“<tt>five</tt>”，而"<tt>two</tt>"和"<tt>seven</tt>"的单元格的宽度也更宽。“<tt>one</tt>,” “<tt>three</tt>,” “<tt>seven</tt>,”
和“<tt>nine</tt>”的单元格是正常的尺寸，并且不受较大的窗口部件的影响。 <span id="line-370" class="anchor"></span><span id="line-371" class="anchor"></span></p>
<p class="line862">图11.8展示了当调整窗口尺寸时，<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>的默认行为——单元格的尺寸不改变。 <span id="line-372" class="anchor"></span><span id="line-373" class="anchor"></span></p>
<p class="line867"><strong>图11.8</strong> <span id="line-374" class="anchor"></span><span id="line-375" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.8.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.8.gif" class="attachment"><span id="line-376" class="anchor"></span><span id="line-377" class="anchor"></span></p>
<p class="line874">例11.6显示了产生了图11.8的代码 <span id="line-378" class="anchor"></span><span id="line-379" class="anchor"></span></p>
<p class="line867"><strong>例11.6</strong>  <strong>创建一个<tt>flex</tt></strong>  <strong><tt>grid</tt></strong>  <strong><tt>sizer</tt></strong> <span id="line-380" class="anchor"></span><span id="line-381" class="anchor"></span></p>
<p class="line867"><span id="line-382" class="anchor"></span><span id="line-383" class="anchor"></span><span id="line-384" class="anchor"></span><span id="line-385" class="anchor"></span><span id="line-386" class="anchor"></span><span id="line-387" class="anchor"></span><span id="line-388" class="anchor"></span><span id="line-389" class="anchor"></span><span id="line-390" class="anchor"></span><span id="line-391" class="anchor"></span><span id="line-392" class="anchor"></span><span id="line-393" class="anchor"></span><span id="line-394" class="anchor"></span><span id="line-395" class="anchor"></span><span id="line-396" class="anchor"></span><span id="line-397" class="anchor"></span><span id="line-398" class="anchor"></span><span id="line-399" class="anchor"></span><span id="line-400" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-50b0ec132a49bc8e24706e544f13b7fd68ad8485_012\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-50b0ec132a49bc8e24706e544f13b7fd68ad8485_012', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-50b0ec132a49bc8e24706e544f13b7fd68ad8485_012" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"FlexGridSizer"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">FlexGridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   9 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">center</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">FindWindowByName</span><span class="Operator">(</span><span class="String">"five"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">center</span><span class="Operator">.</span><span class="ID">SetMinSize</span><span class="Operator">(</span><span class="Operator">(</span><span class="Number">150</span><span class="Operator">,</span><span class="Number">50</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span><span class="ID">TestFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-401" class="anchor"></span><p class="line862">一个<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>是<tt>wx.FlexGridSizer</tt>的一个实例。类<tt>wx.FlexGridSizer</tt>是<tt>wx.GridSizer</tt>的子类，所以<tt>wx.GridSizer</tt>的属性方法依然有效。<tt>wx.FlexGridSizer</tt>的构造函数与其父类的相同： <span id="line-402" class="anchor"></span><span id="line-403" class="anchor"></span></p>
<p class="line867"><tt>wx.FlexGridSizer(rows</tt>, <tt>cols</tt>, <tt>vgap</tt>, <tt>hgap)</tt> <span id="line-404" class="anchor"></span><span id="line-405" class="anchor"></span></p>
<p class="line862">为了当<tt>sizer</tt>扩展时，使一行或列也扩展，你需要使用适当的方法显式地告诉该<tt>sizer</tt>该行或列是可扩展的： <span id="line-406" class="anchor"></span><span id="line-407" class="anchor"></span></p>
<p class="line867"><span id="line-408" class="anchor"></span><span id="line-409" class="anchor"></span><span id="line-410" class="anchor"></span><span id="line-411" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-c79d191f298d0555d548bcc63bd79f8aa3582ae4_013\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-c79d191f298d0555d548bcc63bd79f8aa3582ae4_013', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-c79d191f298d0555d548bcc63bd79f8aa3582ae4_013" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="ID">idx</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">AddGrowableRow</span><span class="Operator">(</span><span class="ID">idx</span><span class="Operator">,</span> <span class="ID">proportion</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-412" class="anchor"></span><p class="line862">当<tt>sizer</tt>水平扩展时，关于新宽度的默认行为被等同地分配给每个可扩展的列。同样，一个垂直的尺寸调整也被等同地分配给每个可扩展的行。要改变这个默认的行为并且使不同的行和列有不现的扩展比率，你需要使用<tt>proportion</tt>参数。如果<tt>proportion</tt>参数被使用了，那么与该参数相关的新的空间就被分配给了相应的行或列。例如，如果你有两个尺寸可调整的行，并且它们的<tt>proportion</tt>分别是2和1，那么这第一个行将得到新空间的2/3，第二行将得到1/3。图11.9显示使用<tt>proportional</tt>（比列）空间的<tt>flex</tt>
<tt>grid</tt> <tt>sizer</tt>。在这里，中间行和列所占的比例是2和5，两端的行和列所占的比例是1。 <span id="line-413" class="anchor"></span><span id="line-414" class="anchor"></span></p>
<p class="line867"><strong>图11.9</strong> <span id="line-415" class="anchor"></span><span id="line-416" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.8.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.8.gif" class="attachment"><span id="line-417" class="anchor"></span><span id="line-418" class="anchor"></span></p>
<p class="line862">正如你可以看到的，当所有的单元格增大时，中间的行和列的增大是两端的两倍。窗口部件的没有改变尺寸以填表充它们的单元格，虽然可以通过在当它们被添加到<tt>sizer</tt>时使用<tt>wx.EXPAND</tt>来实现。例11.7显示了产生图11.9的代码。 <span id="line-419" class="anchor"></span><span id="line-420" class="anchor"></span></p>
<p class="line867"><strong>例11.7</strong> <span id="line-421" class="anchor"></span><span id="line-422" class="anchor"></span></p>
<p class="line867"><span id="line-423" class="anchor"></span><span id="line-424" class="anchor"></span><span id="line-425" class="anchor"></span><span id="line-426" class="anchor"></span><span id="line-427" class="anchor"></span><span id="line-428" class="anchor"></span><span id="line-429" class="anchor"></span><span id="line-430" class="anchor"></span><span id="line-431" class="anchor"></span><span id="line-432" class="anchor"></span><span id="line-433" class="anchor"></span><span id="line-434" class="anchor"></span><span id="line-435" class="anchor"></span><span id="line-436" class="anchor"></span><span id="line-437" class="anchor"></span><span id="line-438" class="anchor"></span><span id="line-439" class="anchor"></span><span id="line-440" class="anchor"></span><span id="line-441" class="anchor"></span><span id="line-442" class="anchor"></span><span id="line-443" class="anchor"></span><span id="line-444" class="anchor"></span><span id="line-445" class="anchor"></span><span id="line-446" class="anchor"></span><span id="line-447" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-55e822066379e878183b6a0ea6a21751e5902515_014\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-55e822066379e878183b6a0ea6a21751e5902515_014', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-55e822066379e878183b6a0ea6a21751e5902515_014" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span>
<span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Resizing Flex Grid Sizer"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">FlexGridSizer</span><span class="Operator">(</span><span class="ID">rows</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">cols</span><span class="Operator">=</span><span class="Number">3</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">,</span> <span class="ID">vgap</span><span class="Operator">=</span><span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   9 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">center</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">FindWindowByName</span><span class="Operator">(</span><span class="String">"five"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">center</span><span class="Operator">.</span><span class="ID">SetMinSize</span><span class="Operator">(</span><span class="Operator">(</span><span class="Number">150</span><span class="Operator">,</span><span class="Number">50</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">,</span> <span class="Number">2</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">AddGrowableRow</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">AddGrowableRow</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">AddGrowableRow</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  22 </span><span class="ID">TestFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  23 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-448" class="anchor"></span><p class="line874">如果你对一个可扩展的行或列使用了比例尺寸，那么你需要对该方向上的所有可扩展的行或列指定一个比例量，否则你将得到一个糟糕的效果。 <span id="line-449" class="anchor"></span><span id="line-450" class="anchor"></span></p>
<p class="line862">在<tt>flex</tt> <tt>grid</tt>
<tt>sizer</tt>中还有另外一个机制用于控制窗口部件的增长（执不执行先前<tt>AddGrowable</tt>*方法的设置）。默认情况下，比例尺寸适用于<tt>flex</tt> <tt>grid</tt>的两个方向；但是你可以通过使用<tt>SetFlexibleDirection(direction)</tt>方法来指定仅某个方向应该按比例调整尺寸，参数<tt>direction</tt>的值可以是：<tt>wx.HORIZONTAL</tt>,  <tt>wx.VERTICAL</tt>,
或<tt>wx.BOTH</tt> （默认值）。然后你可以使用<tt>SetNonFlexibleGrowMode(mode)</tt>方法来指定另一个方向上的行为。例如，如果你调用了<tt>SetFlexibleDirection(wx.HORIZONTAL)</tt>方法，列的行为就遵循<tt>AddGrowableCol()</tt>，然后调用<tt>SetNonFlexibleGrowMode()</tt>来定义行的行为。表11.3显示了<tt>mode</tt>参数的有效值。 <span id="line-451" class="anchor"></span><span id="line-452" class="anchor"></span></p>
<p class="line867"><strong>表11.3</strong> <span id="line-453" class="anchor"></span><span id="line-454" class="anchor"></span></p>
<p class="line867"><tt>wx.FLEX_GROWMODE_ALL</tt>：<tt>flex</tt>
<tt>grid</tt>在没有使用<tt>SetFlexibleDirection</tt>*的方向上等同地调整所有单元格的尺寸。这将覆盖使用<tt>AddGrowable</tt>*方法设置的任何行为——所有的单元格都将被调整尺寸，不管它们的比例或它们是否被指定为可扩展（增长）的。 <span id="line-455" class="anchor"></span><span id="line-456" class="anchor"></span></p>
<p class="line867"><tt>wx.FLEX_GROWMODE_NONE</tt>：在没有使用<tt>SetFlexibleDirection</tt>*的方向上的单元格的尺寸不变化，不管它们是否被指定为可增长的。 <span id="line-457" class="anchor"></span><span id="line-458" class="anchor"></span></p>
<p class="line867"><tt>wx.FLEX_GROWMODE_SPECIFIED</tt>：在没有使用<tt>SetFlexibleDirection</tt>*的方向上，只有那些可增长的单元格才增长。但是<tt>sizer</tt>将忽略任何的比例信息并等同地增长那些单元格。这是一个默认行为。 <span id="line-459" class="anchor"></span><span id="line-460" class="anchor"></span></p>
<p class="line862">上面段落中所讨论的<tt>SetFlexibleDirection</tt>和<tt>SetNonFlexibleGrowMode</tt>方法都有对应的方法：<tt>GetFlexibleDirection()</tt>和<tt>GetNonFlexibleGrowMode()</tt>，它们返回整型标记。在上表中要强调的是，任何使用这些方法来指定的设置将取代通过<tt>AddGrowableCol()</tt>和<tt>AddGrowableRow()</tt>创建的设置。 <span id="line-461" class="anchor"></span><span id="line-462" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-bc0d65d65db8f2a18cadf007de59be462007ce27">1.3.2. 什么是grid bag  sizer?</h3>
<span id="line-463" class="anchor"></span><p class="line867"><tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>是对<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>进一步的增强。在<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>中有两个新的变化： <span id="line-464" class="anchor"></span><span id="line-465" class="anchor"></span></p>
<p class="line862">1、能够将一个窗口部件添加到一个特定的单元格。
2、能够使一个窗口部件跨越几个单元格（就像<tt>HTML</tt>表单中的表格所能做的一样）。 <span id="line-466" class="anchor"></span><span id="line-467" class="anchor"></span></p>
<p class="line867"><strong>图11.10</strong> <span id="line-468" class="anchor"></span><span id="line-469" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.10.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.10.gif" class="attachment"><span id="line-470" class="anchor"></span><span id="line-471" class="anchor"></span></p>
<p class="line862">图11.10显示了一个<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>的示例。它与本章前面的例子很相似，只是增加了新的窗口部件以展示跨行和跨列。 <span id="line-472" class="anchor"></span><span id="line-473" class="anchor"></span></p>
<p class="line862">例11.8显示了产生图11.10的代码。注意这里的<tt>Add()</tt>方法与以前的看起来有点不同。 <span id="line-474" class="anchor"></span><span id="line-475" class="anchor"></span></p>
<p class="line867"><strong>例11.8 <tt>Grid</tt> <tt>bag</tt> <tt>sizer</tt>示例代码</strong> <span id="line-476" class="anchor"></span><span id="line-477" class="anchor"></span></p>
<p class="line867"><span id="line-478" class="anchor"></span></p>
<pre>#coding=utf-8
<span id="line-479" class="anchor"></span>#!python
<span id="line-480" class="anchor"></span>import wx
<span id="line-481" class="anchor"></span>from blockwindow import BlockWindow
<span id="line-482" class="anchor"></span>labels = "one two three four five six seven eight nine".split()
<span id="line-483" class="anchor"></span>class TestFrame(wx.Frame):
<span id="line-484" class="anchor"></span>    def __init__(self):
<span id="line-485" class="anchor"></span>        wx.Frame.__init__(self, None, -1, "GridBagSizer Test")
<span id="line-486" class="anchor"></span>        sizer = wx.GridBagSizer(hgap=5, vgap=5)
<span id="line-487" class="anchor"></span>        for col in range(3):
<span id="line-488" class="anchor"></span>            for row in range(3):
<span id="line-489" class="anchor"></span>                bw = BlockWindow(self, label=labels[row*3 + col])
<span id="line-490" class="anchor"></span>                sizer.Add(bw, pos=(row,col))
<span id="line-491" class="anchor"></span>        # 跨行
<span id="line-492" class="anchor"></span>        bw = BlockWindow(self, label="span 3 rows")
<span id="line-493" class="anchor"></span>        sizer.Add(bw, pos=(0,3), span=(3,1), flag=wx.EXPAND)
<span id="line-494" class="anchor"></span>        # 跨列
<span id="line-495" class="anchor"></span>        bw = BlockWindow(self, label="span all columns")
<span id="line-496" class="anchor"></span>        sizer.Add(bw, pos=(3,0), span=(1,4), flag=wx.EXPAND)
<span id="line-497" class="anchor"></span>        # 使最后的行和列可增长
<span id="line-498" class="anchor"></span>        sizer.AddGrowableCol(3)
<span id="line-499" class="anchor"></span>        sizer.AddGrowableRow(3)
<span id="line-500" class="anchor"></span>        self.SetSizer(sizer)
<span id="line-501" class="anchor"></span>        self.Fit()
<span id="line-502" class="anchor"></span>app = wx.PySimpleApp()
<span id="line-503" class="anchor"></span>TestFrame().Show()
<span id="line-504" class="anchor"></span>app.MainLoop()
<span id="line-505" class="anchor"></span></pre>
<span id="line-506" class="anchor"></span><p class="line867"><tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>是<tt>wx.GridBagSizer</tt>的实例，<tt>wx.GridBagSizer</tt>是<tt>wx.FlexGridSizer</tt>的一个子类。这意味着所有<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>的属性，<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>都适用。 <span id="line-507" class="anchor"></span><span id="line-508" class="anchor"></span></p>
<p class="line867"><tt>wx.GridBagSizer</tt>的构造函数与它的父类有点不同： <span id="line-509" class="anchor"></span><span id="line-510" class="anchor"></span></p>
<p class="line867"><span id="line-511" class="anchor"></span><span id="line-512" class="anchor"></span><span id="line-513" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-8d24780fec71337c326de41c51c5f8079d7fb45b_015\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-8d24780fec71337c326de41c51c5f8079d7fb45b_015', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-8d24780fec71337c326de41c51c5f8079d7fb45b_015" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">GridBagSizer</span><span class="Operator">(</span><span class="ID">vgap</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">,</span> <span class="ID">hgap</span><span class="Operator">=</span><span class="Number">0</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-514" class="anchor"></span><p class="line862">在一个<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>中，你不必去指定行和列的数量，因为你可以直接将子项目添加进特定的单元格——<tt>sizer</tt>将据此计算出网格的尺度。 <span id="line-515" class="anchor"></span><span id="line-516" class="anchor"></span></p>
<p class="line867"><strong>在<tt>grid</tt></strong>  <strong><tt>bag</tt></strong>  <strong><tt>sizer</tt>上使用<tt>Add()</tt>方法</strong> <span id="line-517" class="anchor"></span><span id="line-518" class="anchor"></span></p>
<p class="line862">对于<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>，<tt>Add()</tt>方法与别的<tt>sizer</tt>不同。它有四个可选的形式：
<span id="line-519" class="anchor"></span><span id="line-520" class="anchor"></span></p>
<p class="line867"><span id="line-521" class="anchor"></span></p>
<pre>1 Add(window, pos, span=wx.DefaultSpan, flag=0, border=0,
<span id="line-522" class="anchor"></span>userData=None)
<span id="line-523" class="anchor"></span>2 Add(sizer, pos, span=wx.DefaultSpan, flag=0, border=0,
<span id="line-524" class="anchor"></span>userData=None)
<span id="line-525" class="anchor"></span>3 Add(size, pos, span=wx.DefaultSpan, flag=0, border=0,
<span id="line-526" class="anchor"></span>userData=None)
<span id="line-527" class="anchor"></span>4 AddItem(item)
<span id="line-528" class="anchor"></span></pre>
<span id="line-529" class="anchor"></span><p class="line862">这些看起来应该很熟悉，在运行上也与通常的<tt>sizer</tt>的方法相似。<tt>window</tt>, <tt>sizer</tt>, <tt>size</tt>, <tt>flag</tt>, <tt>border</tt>,
和<tt>userData</tt>参数的行为与通常<tt>sizer</tt>的方法中的是相同的。<tt>pos</tt>参数代表<tt>sizer</tt>中的窗口部件要赋予的单元格。技术上讲，<tt>pos</tt>参数是类<tt>wx.GBPosition</tt>的一个实例，但是通过<tt>wxPython</tt>变换，你可以仅传递一个(行,列)形式的元组，<tt>grid</tt> <tt>bag</tt>的左上角是(0,0)。 <span id="line-530" class="anchor"></span><span id="line-531" class="anchor"></span></p>
<p class="line862">同样，<tt>span</tt>参数代表窗口部件应该占据的行和列的数量。它是类<tt>wx.GBSpan</tt>的一个实例，但是，<tt>wxPython</tt>也使你能够传递一个(行的范围，列的范围)形式的元组。如果跨度没有指定，那么默认值是(1,1)，这意味该窗口部件在两个方向都只能占据一个单元格。例如，要在第二行第一列放置一个窗口部件，并且使它占据三行两列，那么你将这样调用：<tt>Add(widget</tt>,
(1, 0), (3, 2))（索引是从0开始的）。 <span id="line-532" class="anchor"></span><span id="line-533" class="anchor"></span></p>
<p class="line867"><tt>Additem</tt>方法的<tt>item</tt>参数是类<tt>wx.GBSizerItem</tt>的一个实例，它包含了<tt>grid</tt> <tt>bag</tt>
<tt>sizer</tt>放置项目所需要的全部信息。你不太可能需要直接去创建一个<tt>wx.GBSizerItem</tt>。如果你想去创建一个，那么它的构造函数的参数与<tt>grid</tt> <tt>bag</tt>
<tt>sizer</tt>的其它<tt>Add()</tt>方法相同。一旦你有了一个<tt>wx.GBSizerItem</tt>，这儿有许多的<tt>get</tt>*方法使你能够访问项目的属性，也许这最有用的是<tt>GetWindow()</tt>，它返回实际显示的窗口部件。 <span id="line-534" class="anchor"></span><span id="line-535" class="anchor"></span></p>
<p class="line862">由于项目是使用行和列的索引以及跨度被添加到一个<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>的，所以项目被添加的顺序不必按照其它<tt>sizer</tt>所要求的对应它们的显示顺序。这使得跟踪哪个项实际显示在哪个单元格有一点头痛。表11.4列出了几个方法，<tt>grid</tt> <tt>bag</tt> <tt>sizer</tt>通过它们来使你对项目的跟踪较为容易。 <span id="line-536" class="anchor"></span><span id="line-537" class="anchor"></span></p>
<p class="line867"><strong>表11.4 <tt>Grid</tt> <tt>bag</tt> <tt>sizer</tt> 管理项目的方法</strong> <span id="line-538" class="anchor"></span><span id="line-539" class="anchor"></span></p>
<p class="line867"><tt>CheckForIntersection(item</tt>,<tt>excludeItem</tt>=<tt>None)</tt>
<tt>CheckForIntersection(pos</tt>,<tt>span</tt>,
<tt>excludeItem</tt>=<tt>None)</tt>：将给定的项目或给定的位置和跨度同<tt>sizer</tt>中的项目进行比对。如果有任一项与给定项目的位置或给定的位置和跨度重叠，则返回<tt>True</tt>。<tt>excludeItem</tt>是一个可选的项，它不被包括在比对中（或许是因为它正在测试中）。<tt>pos</tt>参数是一个<tt>wx.GBPosition</tt>或一个元组。<tt>span</tt>参数是一个<tt>wx.GPSpan</tt>或一个元组。
<span id="line-540" class="anchor"></span><span id="line-541" class="anchor"></span></p>
<p class="line867"><tt>FindItem(window)</tt> <tt>FindItem(sizer)</tt>：返回对应于给定的窗口或<tt>sizer</tt>的<tt>wx.GBSizerItem</tt>。如果窗口或<tt>sizer</tt>不在<tt>grid</tt> <tt>bag</tt>中则返回<tt>None</tt>。这个方法不递归检查其中的子<tt>sizer</tt>。 <span id="line-542" class="anchor"></span><span id="line-543" class="anchor"></span></p>
<p class="line867"><tt>FindItemAtPoint(pt)</tt>：<tt>pt</tt>参数是对应于所包含的框架的坐标的一个<tt>wx.Point</tt>实例或一个<tt>Python</tt>元组。这个方法返回位于该点的<tt>wx.GBSizerItem</tt> 。如果这个位置在框架的边界之外或如果该点没有<tt>sizer</tt>项目，则返回<tt>None</tt>。
<span id="line-544" class="anchor"></span><span id="line-545" class="anchor"></span></p>
<p class="line867"><tt>FindItemAtPosition(pos)</tt>：该方法返回位于给定单元格位置的<tt>wx.GBSizerItem</tt>，参数<tt>pos</tt>是一个<tt>wx.GBPosition</tt>或一个<tt>Python</tt>元组。如果该位置在<tt>sizer</tt>的范围外或该位置没有项目，则返回<tt>None</tt>。 <span id="line-546" class="anchor"></span><span id="line-547" class="anchor"></span></p>
<p class="line867"><tt>FindItemWithData(userData)</tt>：返回<tt>grid</tt> <tt>bag</tt>中带有给定的<tt>userData</tt>对象的一个项目的<tt>wx.GBSizerItem</tt>。 <span id="line-548" class="anchor"></span><span id="line-549" class="anchor"></span></p>
<p class="line867"><tt>Grid</tt>
<tt>bag</tt>也有一对能够用于处理单元格尺寸和项目位置的属性。在<tt>grid</tt> <tt>bag</tt>被布局好并显示在屏幕上后，你可以使用方法<tt>GetCellSize(row</tt>,
<tt>col)</tt>来获取给定的单元格显示在屏幕上的尺寸。这个尺寸包括了由<tt>sizer</tt>本身所管理的水平和垂直的间隔。你可以使用方法<tt>GetEmptyCellSize()</tt>得到一个空单元格的尺寸，并且你可以使用<tt>SetEmptyCellSize(sz)</tt>改变该属性，这里的<tt>sz</tt>是一个<tt>wx.Size</tt>对象或一个<tt>Python</tt>元组。 <span id="line-550" class="anchor"></span><span id="line-551" class="anchor"></span></p>
<p class="line862">你也可以使用方法<tt>GetItemPosition()</tt>和<tt>GetItemSpan()</tt>来得到<tt>grid</tt> <tt>bag</tt>中的一个对象的位置或跨度。这两个方法要求一个窗口，一个<tt>sizer</tt>或一个索引作为参数。这个索引参数与<tt>sizer</tt>的<tt>Add()</tt>列表中的索引相对应，这个索引在<tt>grid</tt>
<tt>bag</tt>的上下文中没多大意思。上面的两个<tt>get</tt>*方法都有对应的<tt>set</tt>*方法，<tt>SetItemPosition(window</tt>, <tt>pos)</tt>和<tt>SetItemSpan(window</tt>,
<tt>span)</tt>，这两个方法的第一个参数可以是<tt>window</tt>,<tt>sizer</tt>,或<tt>index</tt>，第二个参数是一个<tt>Python</tt>元组或一个<tt>wx.GBPosition</tt>或<tt>wx.GBSpan</tt>对象。 <span id="line-552" class="anchor"></span><span id="line-553" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-af09e9c27edb728c13c67129cd09286460d07941">1.3.3. 什么是box</h3>
<span id="line-554" class="anchor"></span><p class="line867"><strong><tt>sizer</tt>？</strong> <span id="line-555" class="anchor"></span><span id="line-556" class="anchor"></span></p>
<p class="line867"><tt>box</tt> <tt>sizer</tt>是<tt>wxPython</tt>所提供的<tt>sizer</tt>中的最简单和最灵活的<tt>sizer</tt>。一个<tt>box</tt>
<tt>sizer</tt>是一个垂直列或水平行，窗口部件在其中从左至右或从上到下布置在一条线上。虽然这听起来好像用处太简单，但是来自相互之间嵌套<tt>sizer</tt>的能力使你能够在每行或每列很容易放置不同数量的项目。由于每个<tt>sizer</tt>都是一个独立的实体，因此你的布局就有了更多的灵活性。对于大多数的应用程序，一个嵌套有水平<tt>sizer</tt>的垂直<tt>sizer</tt>将使你能够创建你所需要的布局。
<span id="line-557" class="anchor"></span><span id="line-558" class="anchor"></span></p>
<p class="line862">图11.11-11.14展示了几个简单的<tt>box</tt> <tt>sizer</tt>的例子。图中所展示的各个框架我们都是手动调整了大小的，以便展示每个<tt>sizer</tt>是如何响应框架的增大的。图11.11显示了一个水平的<tt>box</tt> <tt>sizer</tt>，图11.12在一个垂直的<tt>box</tt>
<tt>sizer</tt>显示了现图11.11相同的窗口部件。 <span id="line-559" class="anchor"></span><span id="line-560" class="anchor"></span></p>
<p class="line867"><strong>图11.11</strong> <span id="line-561" class="anchor"></span><span id="line-562" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.11.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.11.gif" class="attachment"><span id="line-563" class="anchor"></span><span id="line-564" class="anchor"></span></p>
<p class="line867"><strong>图11.12</strong> <span id="line-565" class="anchor"></span><span id="line-566" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.12.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.12.gif" class="attachment"><span id="line-567" class="anchor"></span><span id="line-568" class="anchor"></span></p>
<p class="line862">图11.13显示了一个垂直的<tt>sizer</tt>，其中一个窗口部件被设置成可扩展并填充有效的垂直空间。图11.14展示了一个垂直的<tt>sizer</tt>，其中的两个窗口部件设置为按不同的比例占据有效的垂直空间。 <span id="line-569" class="anchor"></span><span id="line-570" class="anchor"></span></p>
<p class="line867"><strong>图11.13</strong> <span id="line-571" class="anchor"></span><span id="line-572" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.13.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.13.gif" class="attachment"><span id="line-573" class="anchor"></span><span id="line-574" class="anchor"></span></p>
<p class="line867"><strong>图11.14</strong>
<span id="line-575" class="anchor"></span><span id="line-576" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.14.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.14.gif" class="attachment"><span id="line-577" class="anchor"></span><span id="line-578" class="anchor"></span></p>
<p class="line862">生成以上四个<tt>sizer</tt>框架的示例代码如例11.9所示。 <span id="line-579" class="anchor"></span><span id="line-580" class="anchor"></span></p>
<p class="line867"><strong>例11.9</strong>  <strong>产生多个<tt>box</tt></strong>  <strong><tt>sizer</tt></strong> <span id="line-581" class="anchor"></span><span id="line-582" class="anchor"></span></p>
<p class="line867"><span id="line-583" class="anchor"></span><span id="line-584" class="anchor"></span><span id="line-585" class="anchor"></span><span id="line-586" class="anchor"></span><span id="line-587" class="anchor"></span><span id="line-588" class="anchor"></span><span id="line-589" class="anchor"></span><span id="line-590" class="anchor"></span><span id="line-591" class="anchor"></span><span id="line-592" class="anchor"></span><span id="line-593" class="anchor"></span><span id="line-594" class="anchor"></span><span id="line-595" class="anchor"></span><span id="line-596" class="anchor"></span><span id="line-597" class="anchor"></span><span id="line-598" class="anchor"></span><span id="line-599" class="anchor"></span><span id="line-600" class="anchor"></span><span id="line-601" class="anchor"></span><span id="line-602" class="anchor"></span><span id="line-603" class="anchor"></span><span id="line-604" class="anchor"></span><span id="line-605" class="anchor"></span><span id="line-606" class="anchor"></span><span id="line-607" class="anchor"></span><span id="line-608" class="anchor"></span><span id="line-609" class="anchor"></span><span id="line-610" class="anchor"></span><span id="line-611" class="anchor"></span><span id="line-612" class="anchor"></span><span id="line-613" class="anchor"></span><span id="line-614" class="anchor"></span><span id="line-615" class="anchor"></span><span id="line-616" class="anchor"></span><span id="line-617" class="anchor"></span><span id="line-618" class="anchor"></span><span id="line-619" class="anchor"></span><span id="line-620" class="anchor"></span><span id="line-621" class="anchor"></span><span id="line-622" class="anchor"></span><span id="line-623" class="anchor"></span><span id="line-624" class="anchor"></span><span id="line-625" class="anchor"></span><span id="line-626" class="anchor"></span><span id="line-627" class="anchor"></span><span id="line-628" class="anchor"></span><span id="line-629" class="anchor"></span><span id="line-630" class="anchor"></span><span id="line-631" class="anchor"></span><span id="line-632" class="anchor"></span><span id="line-633" class="anchor"></span><span id="line-634" class="anchor"></span><span id="line-635" class="anchor"></span><span id="line-636" class="anchor"></span><span id="line-637" class="anchor"></span><span id="line-638" class="anchor"></span><span id="line-639" class="anchor"></span><span id="line-640" class="anchor"></span><span id="line-641" class="anchor"></span><span id="line-642" class="anchor"></span><span id="line-643" class="anchor"></span><span id="line-644" class="anchor"></span><span id="line-645" class="anchor"></span><span id="line-646" class="anchor"></span><span id="line-647" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-42798ad8615d16c9fb4cfb04f52ae801865b1049_016\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-42798ad8615d16c9fb4cfb04f52ae801865b1049_016', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-42798ad8615d16c9fb4cfb04f52ae801865b1049_016" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ID">title</span> <span class="Operator">=</span> <span class="String">"none"</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">title</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">CreateSizerAndWindows</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   9 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span><span class="ResWord">class</span> <span class="ID">VBoxSizerFrame</span><span class="Operator">(</span><span class="ID">TestFrame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  12 </span>    <span class="ID">title</span> <span class="Operator">=</span> <span class="String">"Vertical BoxSizer"</span></span>
<span class="line"><span class="LineNumber">  13 </span>    <span class="ResWord">def</span> <span class="ID">CreateSizerAndWindows</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  16 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">200</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ResWord">return</span> <span class="ID">sizer</span></span>
<span class="line"><span class="LineNumber">  19 </span><span class="ResWord">class</span> <span class="ID">HBoxSizerFrame</span><span class="Operator">(</span><span class="ID">TestFrame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  20 </span>    <span class="ID">title</span> <span class="Operator">=</span> <span class="String">"Horizontal BoxSizer"</span></span>
<span class="line"><span class="LineNumber">  21 </span>    <span class="ResWord">def</span> <span class="ID">CreateSizerAndWindows</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HORIZONTAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  23 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  24 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">75</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span>        <span class="ResWord">return</span> <span class="ID">sizer</span></span>
<span class="line"><span class="LineNumber">  27 </span><span class="ResWord">class</span> <span class="ID">VBoxSizerStretchableFrame</span><span class="Operator">(</span><span class="ID">TestFrame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  28 </span>    <span class="ID">title</span> <span class="Operator">=</span> <span class="String">"Stretchable BoxSizer"</span></span>
<span class="line"><span class="LineNumber">  29 </span>    <span class="ResWord">def</span> <span class="ID">CreateSizerAndWindows</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  30 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  31 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  32 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">200</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="Comment"># Add an item that takes all the free space</span></span>
<span class="line"><span class="LineNumber">  35 </span><span class="Comment"></span>        <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="String">"gets all free space"</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">200</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  36 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  37 </span>        <span class="ResWord">return</span> <span class="ID">sizer</span></span>
<span class="line"><span class="LineNumber">  38 </span><span class="ResWord">class</span> <span class="ID">VBoxSizerMultiProportionalFrame</span><span class="Operator">(</span><span class="ID">TestFrame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  39 </span>    <span class="ID">title</span> <span class="Operator">=</span> <span class="String">"Proportional BoxSizer"</span></span>
<span class="line"><span class="LineNumber">  40 </span>    <span class="ResWord">def</span> <span class="ID">CreateSizerAndWindows</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  41 </span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  42 </span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">labels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  43 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">200</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  44 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  45 </span>        <span class="Comment"># Add an item that takes one share of the free space</span></span>
<span class="line"><span class="LineNumber">  46 </span><span class="Comment"></span>        <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  47 </span>                <span class="ID">label</span><span class="Operator">=</span><span class="String">"gets 1/3 of the free space"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  48 </span>                <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">200</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  49 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  50 </span>        <span class="Comment"># Add an item that takes 2 shares of the free space</span></span>
<span class="line"><span class="LineNumber">  51 </span><span class="Comment"></span>        <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  52 </span>                <span class="ID">label</span><span class="Operator">=</span><span class="String">"gets 2/3 of the free space"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  53 </span>                <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">200</span><span class="Operator">,</span><span class="Number">30</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  54 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">2</span><span class="Operator">,</span> <span class="ID">flag</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  55 </span>        <span class="ResWord">return</span> <span class="ID">sizer</span></span>
<span class="line"><span class="LineNumber">  56 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  57 </span><span class="ID">frameList</span> <span class="Operator">=</span> <span class="Operator">[</span><span class="ID">VBoxSizerFrame</span><span class="Operator">,</span> <span class="ID">HBoxSizerFrame</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  58 </span>             <span class="ID">VBoxSizerStretchableFrame</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  59 </span>             <span class="ID">VBoxSizerMultiProportionalFrame</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber">  60 </span><span class="ResWord">for</span> <span class="ID">klass</span> <span class="ResWord">in</span> <span class="ID">frameList</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  61 </span>    <span class="ID">frame</span> <span class="Operator">=</span> <span class="ID">klass</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  62 </span>    <span class="ID">frame</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-648" class="anchor"></span><p class="line867"><tt>box</tt> <tt>sizer</tt>是类<tt>wx.BoxSizer</tt>的实例，<tt>wx.BoxSizer</tt>是<tt>wx.Sizer</tt>的子类，相对于<tt>wx.Sizer</tt>，<tt>wx.BoxSizer</tt>几乎没有增加新的方法。<tt>wx.BoxSizer</tt>的构造函数有一个参数： <span id="line-649" class="anchor"></span><span id="line-650" class="anchor"></span></p>
<p class="line867"><span id="line-651" class="anchor"></span><span id="line-652" class="anchor"></span><span id="line-653" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-5a05a9387dbe662f1debfd0ce5c0cfc7eaf210d1_017\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-5a05a9387dbe662f1debfd0ce5c0cfc7eaf210d1_017', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-5a05a9387dbe662f1debfd0ce5c0cfc7eaf210d1_017" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">orient</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-654" class="anchor"></span><p class="line862">参数<tt>orient</tt>代表该<tt>sizer</tt>的方向，它的取值可以是<tt>wx.VERTICAL</tt>或<tt>wx.HORIZONTAL</tt>。对于<tt>box</tt>
<tt>sizer</tt>所定义的唯一一个新的方法是<tt>GetOrientation()</tt>，它返回构造函数中<tt>orient</tt>的整数值。一旦一个<tt>box</tt> <tt>sizer</tt>被创建后，你就不能改变它的方向了。<tt>box</tt> <tt>sizer</tt>的其它的函数使用本章早先所讨论的一般的<tt>sizer</tt>的方法。 <span id="line-655" class="anchor"></span><span id="line-656" class="anchor"></span></p>
<p class="line867"><tt>box</tt>
<tt>sizer</tt>的布局算法对待该<tt>sizer</tt>的主方向（当构建的时候已被它的方向参数所定义）和次要方向是不同的。特别地，<tt>proportion</tt>参数只适用于当<tt>sizer</tt>沿主方向伸缩时，而<tt>wx.EXPAND</tt>标记仅适用于当<tt>sizer</tt>的尺寸在次方向上变化时。换句话说，当一个垂直的<tt>box</tt>
<tt>sizer</tt>被垂直地绘制时，传递给每个<tt>Add()</tt>方法调用的参数<tt>proportion</tt>决定了每个项目将如何垂直地伸缩。除了影响<tt>sizer</tt>和它的项目的水平增长外，参数<tt>proportion</tt>以同样的方式影响水平的<tt>box</tt>
<tt>sizer</tt>。在另一方面，次方向的增长是由对项目所使用的<tt>wx.EXPAND</tt>标记来控制的，所以，如果它们设置了<tt>wx.EXPAND</tt>标记的话，在一个垂直的<tt>box</tt> <tt>sizer</tt>中的项目将只在水平方向增长。否则这些项目保持它们的最小或最合适的尺寸。图6.7演示了这个过程。 <span id="line-657" class="anchor"></span><span id="line-658" class="anchor"></span></p>
<p class="line862">在<tt>box</tt> <tt>sizer</tt>中，项目的比例增长类似于<tt>flex</tt> <tt>grid</tt> <tt>sizer</tt>，但有一些例外。第一，<tt>box</tt> <tt>sizer</tt>的比例行为是在窗口部件被添加到该<tt>sizer</tt>时，使用<tt>proportion</tt>参数被确定的——你无需像<tt>flex</tt>
<tt>grid</tt> <tt>sizer</tt>那样单独地指定它的增长性。第二，比例为0的行为是不同的。在<tt>box</tt> <tt>sizer</tt>中，0比例意味着该窗口部件在主方向上不将根据它的最小或最合适尺寸被调整尺寸，但是如果<tt>wx.EXPAND</tt>标记被使用了的话，它仍可以在次方向上增长。当<tt>box</tt>
<tt>sizer</tt>在主方向上计算它的项目的布局时，它首先合计固定尺寸的项目所需要的空间，这些固定尺寸的项目，它们的比例为0。余下的空间按项目的比例分配。 <span id="line-659" class="anchor"></span><span id="line-660" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-bfea748837095776adb31cdddd7c19dd11768192">1.3.4. 什么是static</h3>
<span id="line-661" class="anchor"></span><p class="line867"><strong><tt>box</tt></strong>  <strong><tt>sizer</tt>?</strong> <span id="line-662" class="anchor"></span><span id="line-663" class="anchor"></span></p>
<p class="line862">一个<tt>static</tt> <tt>box</tt> <tt>sizer</tt>合并了<tt>box</tt>
<tt>sizer</tt>和静态框（<tt>static</tt> <tt>box</tt>），静态框在<tt>sizer</tt>的周围提供了一个漂亮的边框和文本标签。图11.15显示了三个<tt>static</tt> <tt>box</tt> <tt>sizer</tt>。 <span id="line-664" class="anchor"></span><span id="line-665" class="anchor"></span></p>
<p class="line867"><strong>图11.15</strong> <span id="line-666" class="anchor"></span><span id="line-667" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.15.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.15.gif" class="attachment"><span id="line-668" class="anchor"></span><span id="line-669" class="anchor"></span></p>
<p class="line862">例11.10显示了产生上图的代码。这里有三个值得注意的事件。首先你必须单独于<tt>sizer</tt>创建静态框对象，第二是这个例子展示了如何使用嵌套的<tt>box</tt> <tt>sizer</tt>。本例中，三个垂直的<tt>static</tt> <tt>box</tt> <tt>sizer</tt>被放置于一个水平的<tt>box</tt> <tt>sizer</tt>中。
<span id="line-670" class="anchor"></span><span id="line-671" class="anchor"></span></p>
<p class="line867"><strong>例11.10</strong>  <strong><tt>static</tt></strong>  <strong><tt>box</tt></strong>  <strong><tt>sizer</tt>的一个例子</strong> <span id="line-672" class="anchor"></span><span id="line-673" class="anchor"></span></p>
<p class="line867"><span id="line-674" class="anchor"></span><span id="line-675" class="anchor"></span><span id="line-676" class="anchor"></span><span id="line-677" class="anchor"></span><span id="line-678" class="anchor"></span><span id="line-679" class="anchor"></span><span id="line-680" class="anchor"></span><span id="line-681" class="anchor"></span><span id="line-682" class="anchor"></span><span id="line-683" class="anchor"></span><span id="line-684" class="anchor"></span><span id="line-685" class="anchor"></span><span id="line-686" class="anchor"></span><span id="line-687" class="anchor"></span><span id="line-688" class="anchor"></span><span id="line-689" class="anchor"></span><span id="line-690" class="anchor"></span><span id="line-691" class="anchor"></span><span id="line-692" class="anchor"></span><span id="line-693" class="anchor"></span><span id="line-694" class="anchor"></span><span id="line-695" class="anchor"></span><span id="line-696" class="anchor"></span><span id="line-697" class="anchor"></span><span id="line-698" class="anchor"></span><span id="line-699" class="anchor"></span><span id="line-700" class="anchor"></span><span id="line-701" class="anchor"></span><span id="line-702" class="anchor"></span><span id="line-703" class="anchor"></span><span id="line-704" class="anchor"></span><span id="line-705" class="anchor"></span><span id="line-706" class="anchor"></span><span id="line-707" class="anchor"></span><span id="line-708" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-a2c7e449f2481733111f97876aed2ae852f8a848_018\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-a2c7e449f2481733111f97876aed2ae852f8a848_018', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-a2c7e449f2481733111f97876aed2ae852f8a848_018" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">from</span> <span class="ID">blockwindow</span> <span class="ResWord">import</span> <span class="ID">BlockWindow</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">labels</span> <span class="Operator">=</span> <span class="String">"one two three four five six seven eight nine"</span><span class="Operator">.</span><span class="ID">split</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">TestFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"StaticBoxSizer Test"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">panel</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="Comment"># make three static boxes with windows positioned inside them</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="Comment"></span>        <span class="ID">box1</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeStaticBoxSizer</span><span class="Operator">(</span><span class="String">"Box 1"</span><span class="Operator">,</span> <span class="ID">labels</span><span class="Operator">[</span><span class="Number">0</span><span class="Operator">:</span><span class="Number">3</span><span class="Operator">]</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">box2</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeStaticBoxSizer</span><span class="Operator">(</span><span class="String">"Box 2"</span><span class="Operator">,</span> <span class="ID">labels</span><span class="Operator">[</span><span class="Number">3</span><span class="Operator">:</span><span class="Number">6</span><span class="Operator">]</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">box3</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">MakeStaticBoxSizer</span><span class="Operator">(</span><span class="String">"Box 3"</span><span class="Operator">,</span> <span class="ID">labels</span><span class="Operator">[</span><span class="Number">6</span><span class="Operator">:</span><span class="Number">9</span><span class="Operator">]</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="Comment"># We can also use a sizer to manage the placement of other</span></span>
<span class="line"><span class="LineNumber">  13 </span><span class="Comment"></span>        <span class="Comment"># sizers (and therefore the windows and sub-sizers that they</span></span>
<span class="line"><span class="LineNumber">  14 </span><span class="Comment"></span>        <span class="Comment"># manage as well.)</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="Comment"></span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HORIZONTAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">box1</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">10</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">box2</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">10</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">box3</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">10</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">panel</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span>    <span class="ResWord">def</span> <span class="ID">MakeStaticBoxSizer</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">boxlabel</span><span class="Operator">,</span> <span class="ID">itemlabels</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="Comment"># first the static box</span></span>
<span class="line"><span class="LineNumber">  23 </span><span class="Comment"></span>        <span class="ID">box</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">StaticBox</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">panel</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">boxlabel</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="Comment"># then the sizer</span></span>
<span class="line"><span class="LineNumber">  25 </span><span class="Comment"></span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">StaticBoxSizer</span><span class="Operator">(</span><span class="ID">box</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span>        <span class="Comment"># then add items to it like normal</span></span>
<span class="line"><span class="LineNumber">  27 </span><span class="Comment"></span>        <span class="ResWord">for</span> <span class="ID">label</span> <span class="ResWord">in</span> <span class="ID">itemlabels</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  28 </span>            <span class="ID">bw</span> <span class="Operator">=</span> <span class="ID">BlockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">panel</span><span class="Operator">,</span> <span class="ID">label</span><span class="Operator">=</span><span class="ID">label</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>            <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">bw</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">2</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  30 </span>        <span class="ResWord">return</span> <span class="ID">sizer</span></span>
<span class="line"><span class="LineNumber">  31 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span><span class="ID">TestFrame</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-709" class="anchor"></span><p class="line867"><tt>static</tt> <tt>box</tt> <tt>sizer</tt>是类<tt>wx.StaticBoxSizer</tt>的实例，<tt>wx.StaticBoxSizer</tt>是<tt>wx.BoxSizer</tt>的子类。它的构造函数要求的参数是静态框和方向： <span id="line-710" class="anchor"></span><span id="line-711" class="anchor"></span></p>
<p class="line867"><tt>wx.StaticBoxSizer(box</tt>, <tt>oient)</tt> <span id="line-712" class="anchor"></span><span id="line-713" class="anchor"></span></p>
<p class="line862">在这个构造函数中，<tt>orient</tt>的意义与原<tt>wx.BoxSizer</tt>相同，<tt>box</tt>参数是一个<tt>wx.StaticBox</tt>。对于<tt>static</tt> <tt>box</tt>
<tt>sizer</tt>所定义的别的方法只有一个：<tt>GetStaticBox()</tt>，它返回用于建造该<tt>sizer</tt>的<tt>wx.StaticBox</tt>。一旦该<tt>sizer</tt>被创建，那么你就不能再改变这个静态框了。 <span id="line-714" class="anchor"></span><span id="line-715" class="anchor"></span></p>
<p class="line867"><tt>wx.StaticBox</tt>类有一个用于<tt>wxPython</tt>控件的标准的构造函数，但是其中许多参数都有默认值，可以忽略。 <span id="line-716" class="anchor"></span><span id="line-717" class="anchor"></span></p>
<p class="line867"><tt>wx.StaticBox(parent</tt>, <tt>id</tt>, <tt>label</tt>,
<tt>pos</tt>=<tt>wx.DefaultPosition</tt>, <span id="line-718" class="anchor"></span><span id="line-719" class="anchor"></span></p>
<ul><li style="list-style-type: none;"><p class="line891"><tt>size</tt>=<tt>wx.DefaultSize</tt>, <tt>style</tt>=0, <tt>name</tt>="<tt>staticBox</tt>") <span id="line-720" class="anchor"></span></p></li></ul>
<p class="line862">使用一个<tt>static</tt> <tt>box</tt> <tt>sizer</tt>，你不需要去设置<tt>pos</tt>, <tt>size</tt>, <tt>style</tt>, 或<tt>name</tt>参数，因为位置和尺寸将由<tt>sizer</tt>管理，并且没用单独用于<tt>wx.StaticBox</tt>的样式。这使得构造更简单： <span id="line-721" class="anchor"></span><span id="line-722" class="anchor"></span></p>
<p class="line867"><tt>box</tt> = <tt>wx.StaticBox(self.panel</tt>, -1, <tt>boxlabel)</tt> <span id="line-723" class="anchor"></span><span id="line-724" class="anchor"></span></p>
<p class="line862">到目前为止，我们已经展示了各种<tt>sizer</tt>，我们将给你展示如何在实际的布局中使用它们。对于另外一个用于创建一个复杂布局的例子，请参看第六章。 <span id="line-725" class="anchor"></span><span id="line-726" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-11ef90c5b495f9c562241fdf96022be5470ff08a">1.4. 一个现实中使用sizer的例子</h2>
<span id="line-727" class="anchor"></span><p class="line862">迄今为止，我们所展示的有关<tt>sizer</tt>的例子都是在显示它们的功能方面。下面，我们将展示一个如何使用<tt>sizer</tt>来建造一个真实的布局。图11.16显示了一个使用不同<tt>sizer</tt>建造的复杂程度适中的布局。 <span id="line-728" class="anchor"></span><span id="line-729" class="anchor"></span></p>
<p class="line867"><strong>图11.16</strong> <span id="line-730" class="anchor"></span><span id="line-731" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.16.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.16.gif" class="attachment"><span id="line-732" class="anchor"></span><span id="line-733" class="anchor"></span></p>
<p class="line874">例11.11显示了产生上图的代码。这段代码看起来有点复杂，但是我们将对它分块解读。 <span id="line-734" class="anchor"></span><span id="line-735" class="anchor"></span></p>
<p class="line867"><strong>例11.11</strong>  <strong>用<tt>sizer</tt>来建造地址表单</strong> <span id="line-736" class="anchor"></span><span id="line-737" class="anchor"></span></p>
<p class="line867"><span id="line-738" class="anchor"></span></p>
<pre>#coding=utf-8
<span id="line-739" class="anchor"></span>#!python
<span id="line-740" class="anchor"></span>import wx
<span id="line-741" class="anchor"></span>class TestFrame(wx.Frame):
<span id="line-742" class="anchor"></span>    def __init__(self):
<span id="line-743" class="anchor"></span>        wx.Frame.__init__(self, None, -1, "Real World Test")
<span id="line-744" class="anchor"></span>        panel = wx.Panel(self)
<span id="line-745" class="anchor"></span>        # First create the controls
<span id="line-746" class="anchor"></span>        topLbl = wx.StaticText(panel, -1, "Account Information")#1 创建窗口部件
<span id="line-747" class="anchor"></span>        topLbl.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
<span id="line-748" class="anchor"></span>        nameLbl = wx.StaticText(panel, -1, "Name:")
<span id="line-749" class="anchor"></span>        name = wx.TextCtrl(panel, -1, "");
<span id="line-750" class="anchor"></span>        addrLbl = wx.StaticText(panel, -1, "Address:")
<span id="line-751" class="anchor"></span>        addr1 = wx.TextCtrl(panel, -1, "");
<span id="line-752" class="anchor"></span>        addr2 = wx.TextCtrl(panel, -1, "");
<span id="line-753" class="anchor"></span>        cstLbl = wx.StaticText(panel, -1, "City, State, Zip:")
<span id="line-754" class="anchor"></span>        city  = wx.TextCtrl(panel, -1, "", size=(150,-1));
<span id="line-755" class="anchor"></span>        state = wx.TextCtrl(panel, -1, "", size=(50,-1));
<span id="line-756" class="anchor"></span>        zip   = wx.TextCtrl(panel, -1, "", size=(70,-1));
<span id="line-757" class="anchor"></span>        phoneLbl = wx.StaticText(panel, -1, "Phone:")
<span id="line-758" class="anchor"></span>        phone = wx.TextCtrl(panel, -1, "");
<span id="line-759" class="anchor"></span>        emailLbl = wx.StaticText(panel, -1, "Email:")
<span id="line-760" class="anchor"></span>        email = wx.TextCtrl(panel, -1, "");
<span id="line-761" class="anchor"></span>        saveBtn = wx.Button(panel, -1, "Save")
<span id="line-762" class="anchor"></span>        cancelBtn = wx.Button(panel, -1, "Cancel")
<span id="line-763" class="anchor"></span>        # Now do the layout.
<span id="line-764" class="anchor"></span>        # mainSizer is the top-level one that manages everything
<span id="line-765" class="anchor"></span>#2 垂直的sizer
<span id="line-766" class="anchor"></span>        mainSizer = wx.BoxSizer(wx.VERTICAL)
<span id="line-767" class="anchor"></span>        mainSizer.Add(topLbl, 0, wx.ALL, 5)
<span id="line-768" class="anchor"></span>        mainSizer.Add(wx.StaticLine(panel), 0,
<span id="line-769" class="anchor"></span>                wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
<span id="line-770" class="anchor"></span>        # addrSizer is a grid that holds all of the address info
<span id="line-771" class="anchor"></span>#3 地址列
<span id="line-772" class="anchor"></span>        addrSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
<span id="line-773" class="anchor"></span>        addrSizer.AddGrowableCol(1)
<span id="line-774" class="anchor"></span>        addrSizer.Add(nameLbl, 0,
<span id="line-775" class="anchor"></span>                wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
<span id="line-776" class="anchor"></span>        addrSizer.Add(name, 0, wx.EXPAND)
<span id="line-777" class="anchor"></span>        addrSizer.Add(addrLbl, 0,
<span id="line-778" class="anchor"></span>                wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
<span id="line-779" class="anchor"></span>        addrSizer.Add(addr1, 0, wx.EXPAND)
<span id="line-780" class="anchor"></span>#4 带有空白空间的行
<span id="line-781" class="anchor"></span>        addrSizer.Add((10,10)) # some empty space
<span id="line-782" class="anchor"></span>        addrSizer.Add(addr2, 0, wx.EXPAND)
<span id="line-783" class="anchor"></span>        addrSizer.Add(cstLbl, 0,
<span id="line-784" class="anchor"></span>                wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
<span id="line-785" class="anchor"></span>        # the city, state, zip fields are in a sub-sizer
<span id="line-786" class="anchor"></span>#5 水平嵌套
<span id="line-787" class="anchor"></span>        cstSizer = wx.BoxSizer(wx.HORIZONTAL)
<span id="line-788" class="anchor"></span>        cstSizer.Add(city, 1)
<span id="line-789" class="anchor"></span>        cstSizer.Add(state, 0, wx.LEFT|wx.RIGHT, 5)
<span id="line-790" class="anchor"></span>        cstSizer.Add(zip)
<span id="line-791" class="anchor"></span>        addrSizer.Add(cstSizer, 0, wx.EXPAND)
<span id="line-792" class="anchor"></span>#6 电话和电子邮箱
<span id="line-793" class="anchor"></span>        addrSizer.Add(phoneLbl, 0,
<span id="line-794" class="anchor"></span>                wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
<span id="line-795" class="anchor"></span>        addrSizer.Add(phone, 0, wx.EXPAND)
<span id="line-796" class="anchor"></span>        addrSizer.Add(emailLbl, 0,
<span id="line-797" class="anchor"></span>                wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
<span id="line-798" class="anchor"></span>        addrSizer.Add(email, 0, wx.EXPAND)
<span id="line-799" class="anchor"></span>        # now add the addrSizer to the mainSizer
<span id="line-800" class="anchor"></span>#7 添加Flex sizer
<span id="line-801" class="anchor"></span>        mainSizer.Add(addrSizer, 0, wx.EXPAND|wx.ALL, 10)
<span id="line-802" class="anchor"></span>        # The buttons sizer will put them in a row with resizeable
<span id="line-803" class="anchor"></span>        # gaps between and on either side of the buttons
<span id="line-804" class="anchor"></span>#8 按钮行
<span id="line-805" class="anchor"></span>        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
<span id="line-806" class="anchor"></span>        btnSizer.Add((20,20), 1)
<span id="line-807" class="anchor"></span>        btnSizer.Add(saveBtn)
<span id="line-808" class="anchor"></span>        btnSizer.Add((20,20), 1)
<span id="line-809" class="anchor"></span>        btnSizer.Add(cancelBtn)
<span id="line-810" class="anchor"></span>        btnSizer.Add((20,20), 1)
<span id="line-811" class="anchor"></span>        mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)
<span id="line-812" class="anchor"></span>        panel.SetSizer(mainSizer)
<span id="line-813" class="anchor"></span>        # Fit the frame to the needs of the sizer.  The frame will
<span id="line-814" class="anchor"></span>        # automatically resize the panel as needed.  Also prevent the
<span id="line-815" class="anchor"></span>        # frame from getting smaller than this size.
<span id="line-816" class="anchor"></span>        mainSizer.Fit(self)
<span id="line-817" class="anchor"></span>        mainSizer.SetSizeHints(self)
<span id="line-818" class="anchor"></span>app = wx.PySimpleApp()
<span id="line-819" class="anchor"></span>TestFrame().Show()
<span id="line-820" class="anchor"></span>app.MainLoop()
<span id="line-821" class="anchor"></span></pre>
<span id="line-822" class="anchor"></span><p class="line867"><strong>#1</strong> 代码的第一部分是创建使用在窗口中的窗口部件，它们在这行开始。我们在增加<tt>sizer</tt>前将它们全部创建。 <span id="line-823" class="anchor"></span><span id="line-824" class="anchor"></span></p>
<p class="line867"><strong>#2</strong> 在这个布局中的主<tt>sizer</tt>是<tt>mainSizer</tt>，它是一个竖直的<tt>box</tt> <tt>sizer</tt>。被添加到<tt>mainSizer</tt>的第一个元素是顶部的静态文本标签和一个<tt>static</tt> <tt>line</tt>。 <span id="line-825" class="anchor"></span><span id="line-826" class="anchor"></span></p>
<p class="line867"><strong>#3</strong> 在<tt>box</tt> <tt>sizer</tt>中接下来的元素是<tt>addrSizer</tt>，它是一个<tt>flex</tt> <tt>grid</tt>
<tt>sizer</tt>，它有两列，它两列用于容纳其余的地址信息。<tt>addrSizer</tt>的左列被设计用于静态文本标签，而右列用于得到文本控件。这意味着标签和控件需要被交替的添加，以保证<tt>grid</tt>的正确。你可以看到<tt>nameLbl</tt>, <tt>name</tt>, <tt>addrLbl</tt>, 和<tt>addr1</tt>是首先被添加到该<tt>flex</tt>
<tt>grid</tt>中的四个元素。 <span id="line-827" class="anchor"></span><span id="line-828" class="anchor"></span></p>
<p class="line867"><strong>#4</strong> 这接下来的行是不同的，因为这第二个地址行没有标签，一个(10,10)尺寸的空白块被添加，然后是<tt>addr2</tt>控件。 <span id="line-829" class="anchor"></span><span id="line-830" class="anchor"></span></p>
<p class="line867"><strong>#5</strong> 接下来的行又有所不同，包括“<tt>City</tt>, <tt>State</tt>, <tt>Zip</tt>”的行要求三个不同的文本控件，基于这种情况，我们创建了一个水平的<tt>box</tt>
<tt>sizer</tt>：<tt>cstSizer</tt>。这三个控件被添加给<tt>cstSizer</tt>，然后这个<tt>box</tt> <tt>sizer</tt>被添加到<tt>addrSizer</tt>。 <span id="line-831" class="anchor"></span><span id="line-832" class="anchor"></span></p>
<p class="line867"><strong>#6</strong> 电话和电子邮箱行被添加到<tt>flex</tt>
<tt>sizer</tt>。 <span id="line-833" class="anchor"></span><span id="line-834" class="anchor"></span></p>
<p class="line867"><strong>#7</strong> 有关地址的<tt>flex</tt> <tt>sizer</tt>被正式添加到主<tt>sizer</tt>。 <span id="line-835" class="anchor"></span><span id="line-836" class="anchor"></span></p>
<p class="line867"><strong>#8</strong> 按钮行作为水平<tt>box</tt> <tt>sizer</tt>被添加，其中有一些空白元素以分隔按钮。 <span id="line-837" class="anchor"></span><span id="line-838" class="anchor"></span></p>
<p class="line862">在调整了<tt>sizer</tt>（<tt>mainSizer.Fit(self)</tt>）和防止框架变得更小之后（<tt>mainSizer.SetSizeHints(self)</tt>），元素的布局就结束了。 <span id="line-839" class="anchor"></span><span id="line-840" class="anchor"></span></p>
<p class="line874">在读这接下来的段落或运行这个例子之前，请试着想出该框架将会如何在水平和竖直方向上响应增长。 <span id="line-841" class="anchor"></span><span id="line-842" class="anchor"></span></p>
<p class="line862">如果该窗口在竖直方向上的大小改变了，其中的元素不会移动。这是因为主<tt>sizer</tt>是一个垂直的<tt>box</tt> <tt>sizer</tt>，你是在它的主方向上改变尺寸，它没有一个顶级元素是以大于0的比列被添加的。如果这个窗口在水平方向被调整尺寸，由于这个主<tt>sizer</tt>是一个垂直的<tt>box</tt>
<tt>sizer</tt>，你是在它的次方向改变尺寸，因此它的所有有<tt>wx.EXPAND</tt>标记的元素将水平的伸展。这意味着顶部的标签不增长，但是<tt>static</tt> <tt>line</tt>和子<tt>sizer</tt>将水平的增长。用于地址的<tt>flex</tt> <tt>grid</tt>
<tt>sizer</tt>指定列1是可增长的，这意味着包含文本控件的第二列将增长。在“<tt>City</tt>, <tt>State</tt>, <tt>Zip</tt>”
行内，比列为1的<tt>city</tt>元素将伸展，而<tt>state</tt>和<tt>ZIP</tt>控件将保持尺寸不变。按钮将保持原有的尺寸，因为它们的比列是0，但是按钮所在行的空白空间将等分地占据剩下的空间，因为它们每个的比列都是1。 <span id="line-843" class="anchor"></span><span id="line-844" class="anchor"></span></p>
<p class="line874">因此如果你想出的窗口伸展的样子和下图11.17相同，那么你是正确的。 <span id="line-845" class="anchor"></span><span id="line-846" class="anchor"></span></p>
<p class="line867"><strong>图11.17</strong> <span id="line-847" class="anchor"></span><span id="line-848" class="anchor"></span></p>
<p class="line867"><img title="attachment:w11.17.gif" src="/moin/WxPythonInAction/ChapterEleven?action=AttachFile&amp;do=get&amp;target=w11.17.gif" class="attachment"><span id="line-849" class="anchor"></span><span id="line-850" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-378681f27ad089b21bdca12c74de21e67465a840">1.5. 本章小结</h2>
<span id="line-851" class="anchor"></span><p class="line862">1、<tt>Sizer</tt>是对<tt>wxPython</tt>程序中管理布局问题的解决方法。不用手动指定每个元素在布局中的位置和尺寸，你可以将元素添加到一个<tt>sizer</tt>中，由<tt>sizer</tt>负责将每个元素放置到屏幕上。当用户调整框架的尺寸时，<tt>sizer</tt>管理布局是相当好的。 <span id="line-852" class="anchor"></span><span id="line-853" class="anchor"></span></p>
<p class="line862">2、所有的<tt>wxPython</tt>的<tt>sizer</tt>都是类<tt>wx.Sizer</tt>的一个子类的实例。要使用一个<tt>sizer</tt>，你需要把它与一个容器型的窗口部件关联起来。然后，对于要添加到容器中的子窗口部件，你也必需将它们添加到该<tt>sizer</tt>。最后，调用该<tt>sizer</tt>的<tt>Fit()</tt>方法来触发该<tt>sizer</tt>的算法，以便布局和放置。
<span id="line-854" class="anchor"></span><span id="line-855" class="anchor"></span></p>
<p class="line862">3、所有的<tt>sizer</tt>开始都给它们的孩子以最小的尺寸。每种<tt>sizer</tt>各自使用不同的机制来放置窗口部件，所以相同组的窗口部件放置在不同的<tt>sizer</tt>中时，它们的外观也是不同的。 <span id="line-856" class="anchor"></span><span id="line-857" class="anchor"></span></p>
<p class="line862">4、或许在<tt>wxPython</tt>中，最简单的<tt>sizer</tt>是<tt>grid</tt> <tt>sizer(wx.GridSizer)</tt>。在<tt>grid</tt>
<tt>sizer</tt>中，元素按照它们被添加给<tt>sizer</tt>的顺序被放置在一个二维的网格中，按照从左到右，从上到下的方式排列。通常你负责设定列数，<tt>sizer</tt>确定行数。你能够同时指定行和列，如果你愿意的话。 <span id="line-858" class="anchor"></span><span id="line-859" class="anchor"></span></p>
<p class="line862">5、所有的<tt>sizer</tt>都有各自不同的用来将窗口部件添加到<tt>sizer</tt>的方法。由于窗口部件添加到<tt>sizer</tt>中的顺序对于最后的布局是重要的，所以有不同的方法用来添加一个新的窗口部件到列表中的前面、后面或任意点。在一个窗口部件被添加到<tt>sizer</tt>时，另外的属性可以被设置，它们控制当<tt>sizer</tt>增减时，其中的子元素如何变化。<tt>sizer</tt>也能够被配置来在对象的某些或全部边上放置一个边界间隙。
<span id="line-860" class="anchor"></span><span id="bottom" class="anchor"></span></p>
</div>
</div>
</td>
                    </tr></tbody></table>
</div>
        
        
        
        <td id="portal-column-two">
            <div class="visualPadding">
                <!-- The sticky notes -->
                 
            </div>
        </td>
        
        
        
        <div class="visualClear" id="clear-space-before-footer">
            <!-- -->
        </div>
        <hr class="netscape4">
<div id="portal-footer">
            <p> 中国Zope/Plone用户组 - 站长  ： <a target="_self" href="http://czug.org/blog/panjy"> 潘俊勇 </a>、
             <a target="_self" href="http://czug.org/blog/zhangbingkai"> 张炳凯 </a>
            </p>
        </div>
        <div id="portal-colophon">

            <p>
            本站由<a target="_blank" href="http://zopen.cn" alt="润普公司 内容管理 文档管理 知识管理 项目管理 Zope Plone">
                   润普公司</a>资助，
            采用 <a href="http://opensource.everydo.com/cms/">易度CMS </a> 构建。</p>

            <p class="discreet">
            广而告之：润普公司 <a href="http://everydo.com">易度云办公平台</a>，包括<a href="http://edodocs.com"> 易度文档管理系统 </a>，<a href="http://everydo.com/themes/czug.org/pm.everydo.com%0A">易度项目管理系统 </a> , <a href="http://everydo.com/tour/dept.rst"> 易度部门管理 </a> ，均采用Zope 3/BFG技术开发。
            <br><a target="_blank" href="http://www.miibeian.gov.cn/">沪ICP备05008050</a>
            </p>
            </div>
        
        
        <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script type="text/javascript">
        try {
        var pageTracker = _gat._getTracker("UA-13170837-4");
        pageTracker._trackPageview();
        } catch(err) {}</script>
</body>
</html>
